无法理解涉及静态对象的java程序输出的逻辑
问题描述:
我在阅读有关静态关键字的内容。我试过一个涉及静态对象的程序。 输出根据概念而不同。我不明白为什么我没有得到a20的输出。虽然a1和b1都在控制台上,但不是a20。 为什么我在输出中得到两次a1?无法理解涉及静态对象的java程序输出的逻辑
package j1;
class a {
a(int i) {
System.out.println("a"+1);
}
}
class b {
b(int i) {
System.out.println("b"+1);
}
}
class c {
static a a1=new a(1);
static b b1=new b(1);
c() {
System.out.println("c()");
}
static a a2=new a(20);
}
public class Static {
public static void main(String[] args) {
new c();
}
}
//output:
a1
b1
a1
c()
答
您的构造是
a(int i){
System.out.println("a"+1);
}
它只能打印“A1”,如果你想打印“A20”,你需要我纳入参数到输出。
答
下面的代码应该有所帮助:
void a(int i) {
System.out.println("a" + i);
}
对不起那个打错了!!!!!!!!!! 1个 – MANU 2014-09-13 10:00:56