[英]Making an array object from another Class in Java
Firstly, let me preface this by saying that I'm quite new to Java, so apologies if I mess up on any terminology or anything. 首先,让我先说一下我对Java还是很陌生,如果对任何术语或任何东西弄乱了,我深表歉意。
Now, secondly, on to my question. 现在,第二,我的问题。
I've made a String array in one class('Warranty'): 我在一个类中创建了一个String数组(“保修”):
public class Warranty {
private int monthnum;
private String[] partscovered = new String[5]; {
partscovered[0]= "Engine";
partscovered[1]= "Mirrors";
partscovered[2]= "Electrics";
partscovered[3]= "Body";
partscovered[4]= "Wheels";
}
public Warranty(int monthnum,String[] partscovered) {
this.monthnum = monthnum;
this.partscovered= partscovered;
}
public void displaywarranty() {
System.out.println("*******");
System.out.println(8);
System.out.println(partscovered);
System.out.println("\n");
}
I'm trying to instantiate it in another class('VehicleTest'): 我试图在另一个类('VehicleTest')中实例化它:
public class VehicleTest {
public static void main(String[] args) {
......
Warranty warranty = new Warranty(0,partscovered); //returns an error: 'partscovered cannot be resolved to a variable'
warranty.displaywarranty();
}
}
I want to print the array to the console of my IDE, but I'm a little stuck as to why I'm getting the above error message (ie partscovered cannot be resolved to a variable
). 我想将数组打印到我的IDE的控制台中,但是我为什么会收到上述错误消息(即,被
partscovered cannot be resolved to a variable
),所以我partscovered cannot be resolved to a variable
。 It seems to me as if I've done it right, but clearly I haven't so I'm a little stumped to be honest. 在我看来,似乎我做得对,但显然我没有,所以说实话我有些沮丧。 Any help would be greatly appreciated!
任何帮助将不胜感激! :)
:)
There are three problems with this code. 此代码存在三个问题。
Firstly, the way to print an array is 首先,打印数组的方法是
System.out.println(Arrays.toString(partscovered));
Secondly, you cannot pass partscovered
to the Warrenty
constructor because partscovered
is a field of Warranty
. 其次,您不能将
partscovered
传递给Warrenty
构造函数,因为被发现的partscovered
是Warranty
一个字段。 You would have to make a separate variable. 您将必须创建一个单独的变量。 Something like
就像是
String[] pc = {"Steering Wheel", "Exhaust Pipe"};
Warranty warranty = new Warranty(0, pc);
warranty.displaywarranty();
Alternatively, you could avoid using a variable at all 或者,您可以完全避免使用变量
Warranty warranty = new Warranty(0, new String[]{"Steering Wheel", "Exhaust Pipe"});
Finally, I personally dislike your use of an instance initializer to fill up the array partscovered
with "Engine"
, "Mirrors"
etc. This happens even if you pass an array to the constructor. 最后,我个人不喜欢您使用实例初始化程序来填充用
"Engine"
, "Mirrors"
等partscovered
的数组partscovered
。即使将数组传递给构造函数,也会发生这种情况。 Another way to provide a default array is to write a second constructor: 提供默认数组的另一种方法是编写第二个构造函数:
private String[] partscovered; // This is deliberately not assigned
public Warranty(int monthnum) {
this.monthnum = monthnum;
this.partscovered = new String[]{"Engine", "Mirrors", "Electrics", "Body", "Wheels"};
}
public Warranty(int monthnum,String[] partscovered) {
this.monthnum = monthnum;
this.partscovered = partscovered;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.