class A
{
public int i=1;
}
public class Test extends A{
public int i=2;
public static void main(String [] args)
{
A n = new Test();
A a = new A();
Test t = new Test();
System.out.println(n.i);
System.out.println(a.i);//
System.out.println(t.i);
}
}
运行结果是
1
1
2
为什么n.i输出的是1不是2
{
public int i=1;
}
public class Test extends A{
public int i=2;
public static void main(String [] args)
{
A n = new Test();
A a = new A();
Test t = new Test();
System.out.println(n.i);
System.out.println(a.i);//
System.out.println(t.i);
}
}
运行结果是
1
1
2
为什么n.i输出的是1不是2
class A{
int i = 0;
}
public class Test11 extends A{
int i = 10;
public void pr(){
System.out.println("123");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
A a = new Test11();
a = (Test11)a;
a.pr;//报错
Test11 test11 = new Test11();
test11.pr();
}}
new出来的是父类,经测试,子类中如果有方法,n是没有的
所以n的i是1,因为用的是父类的
A n = new Test();
你返回的是一个你的父亲的类型你怎么奢望他有的你特征,当然是你父亲的特征