public class Father { public void print() { System.out.println("Father:print()"); } }
public class Son extends Father{ // 方法的覆盖:子类重写父类的同名方法 Override public void print() { System.out.println("Son:print()"); } // Father obj1 = son; public void a(Father obj1) { //System.out.println("Son:a()"); obj1.print(); } public static void main(String[] agrs) { // 父类 对象 = new 子类(); Father obj = new Son(); obj.print(); // Son:print() //obj.a(); 只能调用子类覆盖父类的方法 // 子类对象赋给父类 Son son = new Son(); son.a(son); // Son:print() Father obj2 = new Father(); obj2.print(); // Father:print() } }
Son:print()
Son:print()
Father:print()