此时我只是想从我所拥有的很少的东西中得到一些东西,但没有任何效果。
所以我从一个驱动程序类开始:
class TheDriverClass
{
public static void main(String[] args)
{
Phone p = new Phone(5);
System.out.println(p);
// Here it's supposed to return the values of an array with size 5,
//so it should print out 00000
}
}
接下来我有类(class)电话:
class Phone
{
private Phone[] arr; //I'm supposed to keep the array private
public Phone(int theLength){
arr = new Phone[theLength];
return Arrays.toString(arr);
}
}
现在看起来有点可笑,因为我已经不顾一切地想要至少得到一些东西,而且我已经用尽了所有的想法。
应该发生的是,驱动程序类为 Phone 提供一个数字,该数字将用作数组 (arr) 的长度,并且使用该长度初始化数组(数组的所有整数默认为 0)并返回数组的长度与 0 的数量一样多(或者如果我要在数组的不同位置分配不同的值,它将按照数组中放置的顺序遍历并打印每个值)。
最佳答案
你的构造函数不会返回任何东西
public Phone(int theLength){
arr = new Phone[theLength];
}
但是你需要一个 setter/getter
public Phone[] getPhones () {
return arr;
}
通常我会有单独的类来保存Phone
数组,而Phone
对象本身只处理实际手机的功能。
关于java - 如何将数组返回到 Java 中的另一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52711585/