急向高手请教一个构造函数的问题!请帮小弟看看这段程序有什么错误?为什么?
public class StringTest {
int buy;
String[] we;
StringTest()
{
String[] we={ "qw ", "we ", "rt "};
buy=1;
}
public static void main(String args[])
{ StringTest it =new StringTest();
for (int i = 1; i <= 3; i++) {
System.out.println(it.we[i]);
System.out.println(it.buy);
}
}
}
------解决方案--------------------public class StringTest {
int buy;
String[] we;
StringTest()
{
we=new String[]{ "qw ", "we ", "rt "};//对we初始化的时候,不能重新定义String
buy=1;
}
public static void main(String args[])
{ StringTest it =new StringTest();
for (int i = 0; i < 3; i++) {//数组的下标从0开始
System.out.println(it.we[i]);
System.out.println(it.buy);
}
}
}