每天开心一点

Java使用super操作被隐藏的成员变量和方法

2019-11-18 23:23:00    六月    623    来源: https://blog.csdn.net/zqxlonely/article/details/18267811

源代码:

public class Test {
public static void main(String[] args) {
   B b=new B();
   b.m=3;
   b.n=7;
   long resultOne=b.g();
   long resultTwo=b.f();
   long resultThree=b.g();
   System.out.println(resultOne);
   System.out.println(resultTwo);
   System.out.println(resultThree);
  }
}

class A{
   int m=0;
   int n=0;
   long f(){
     return m+n;
    }
}

class B extends A{
    int m=1;
    int n=1;

    long f(){
     long result=0;
     super.m=10;
      super.n=20;
      result=super.f()+(m+n);
     return result;
    }

    long g(){
      long result=0;
      result=super.f();
      return result/2;
    }
}

程序运行结果:


————————————————
版权声明:本文为CSDN博主「zqxLonely」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zqxlonely/article/details/18267811