多态-成员变量访问

匿名 (未验证) 提交于 2019-12-03 00:32:02
成员变量
编译看左边(父类),运行看左边(父类)
成员方法
编译看左边(父类),运行看右边(子类)。动态绑定
静态方法
编译看左边(父类),运行看左边(父类)。
(静态和类相关,算不上重写,所以,访问还是左边的)
只有非静态的成员方法,编译看左边,运行看右边
class Demo2_Polymorphic {     public static void main(String[] args) {         /*Father f = new Son();                 //父类引用指向子类对象         System.out.println(f.num);          Son s = new Son();         System.out.println(s.num);*/          Father f = new Son();         //f.print();         f.method();                         //相当于是Father.method()     } } /* 成员变量 编译看左边(父类),运行看左边(父类) 成员方法 编译看左边(父类),运行看右边(子类)。动态绑定 静态方法 编译看左边(父类),运行看左边(父类)。 (静态和类相关,算不上重写,所以,访问还是左边的) 只有非静态的成员方法,编译看左边,运行看右边  */ class Father {     int num = 10;     public void print() {         System.out.println("father");     }      public static void method() {         System.out.println("father static method");     } }  class Son extends Father {     int num = 20;      public void print() {         System.out.println("son");     }      public static void method() {         System.out.println("son static method");     } }

原文:http://blog.51cto.com/357712148/2131937

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!