今日练习如下:
第1题考核知识点:属性与多态无关
package com.yan.test01;
public class Test01 {
public static void main(String[] args) {
A a = new B();
System.out.println(a.num);
System.out.println(((B)a).num);
System.out.println(((A)((B)a)).num);
System.out.println("-------------------");
B b = new B();
System.out.println(b.num);
System.out.println(((A)b).num);
System.out.println(((B)((A)b)).num);
}
}
class A{
int num = 1;
}
class B extends A{
int num = 2;
}
第2题考核知识点:实例初始化方法,属性与多态无关
package com.yan.test02;
public class Test02 {
public static void main(String[] args) {
Father f = new Son();
System.out.println(f.x);
}
}
class Father{
int x = 10;
public Father(){
this.print();
x = 20