【发布时间】:2023-04-10 00:05:01
【问题描述】:
我教一些学生。
如何访问 A 类的属性 age?
class A {
protected int age;
public A(int age){
this.age = age+2;
}
}
class B extends A{
protected int age;
public B(int age){
super(age);
this.age = age+1;
}
}
class C extends B{
protected int age;
public C(int age){
super(age);
this.age = age;
}
public void showInfo(){
// System.out.println(A.this.age);
System.out.println(super.age);
System.out.println(this.age);
}
}
【问题讨论】:
-
通过将
protected int age;添加到子类中,我认为您正在隐藏超级变量 -
"I works for teaching some students."-- ?? -
@HovercraftFullOfEels 我在一个英语老师的广告中看到了一个非常相似的句子。
-
尝试在 B 类中包含
public void showInfo()并打印super.age,然后在 C 类的重写方法中调用函数super.showinfo() -
@HovercraftFullOfEels 现在好点了吗?这不是一个好问题,但它消除了这个困难。
标签: java inheritance extends