废话不说,大家看程序.
a1public class a1{
public static void main(String[]args){
int a = 123;
}
}a2public class a2 extends a1{
public static void main(String[]args){
a2 t = new a2();
int b = 33;
int c = a+b;
System.out.println(c);
}
}
想请问下高手,既然我a2继承了a1,那么也就继承了a1里的int a =123;那为什么编译的时候显示找不到a变量呢?
我试验过,无论是a+b 还是 super.a+b都不行,这是为什么呢?
期待高手解答下困惑~~~~~~~
a1public class a1{
public static void main(String[]args){
int a = 123;
}
}a2public class a2 extends a1{
public static void main(String[]args){
a2 t = new a2();
int b = 33;
int c = a+b;
System.out.println(c);
}
}
想请问下高手,既然我a2继承了a1,那么也就继承了a1里的int a =123;那为什么编译的时候显示找不到a变量呢?
我试验过,无论是a+b 还是 super.a+b都不行,这是为什么呢?
期待高手解答下困惑~~~~~~~
是局部变量
只在main方法中有效
int a = 123;
}
a是局部变量,在子类中无论如何也不能被子类调用的
public class a2 extends a1{
public static void main(String[]args){
a2 t = new a2();
int b = 33;
int c = a+b;
System.out.println(c);
}
}
在这个时候上面中a2的main方法覆盖了a1中的main方法。