ITPub博客

首页 > IT职业 > IT生活 > 多态-成员变量访问

多态-成员变量访问

IT生活 作者:kalt 时间:2021-09-09 10:07:26 0 删除 编辑

成员变量
编译看左边(父类),运行看左边(父类)
成员方法
编译看左边(父类),运行看右边(子类)。动态绑定
静态方法
编译看左边(父类),运行看左边(父类)。
(静态和类相关,算不上重写,所以,访问还是左边的)
只有非静态的成员方法,编译看左边,运行看右边

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");    }}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1805/viewspace-2816853/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    121
  • 访问量
    892