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

来源:互联网 发布:全球智能手机销售数据 编辑:程序博客网 时间:2024/06/06 18:16

源代码:

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


程序运行结果:


0 0