public class Father {
public int i=10;
public static void main(String[] args) {
Father o1 = new Father();
System.out.println(o1.i);
Father o2= new Son();
System.out.println(o2.i);
Son o3 = new Son();
System.out.println(o3.i);
}
}class Son extends Father{
public int i=20;
}
大家觉得答案是什么
10 20 20
还是 10 10 20
不确定的 运行下 谢谢
有理解的人 帮忙说下原因?
public int i=10;
public static void main(String[] args) {
Father o1 = new Father();
System.out.println(o1.i);
Father o2= new Son();
System.out.println(o2.i);
Son o3 = new Son();
System.out.println(o3.i);
}
}class Son extends Father{
public int i=20;
}
大家觉得答案是什么
10 20 20
还是 10 10 20
不确定的 运行下 谢谢
有理解的人 帮忙说下原因?
变量不会像方法那样被重写,所以声明为Father的对象它们的i都是Father类中i的值10,不会因为继承而变成Son类中i的值20
Father o2= new Son();
调用的还是Father类