【发布时间】:2015-11-11 14:56:58
【问题描述】:
我在理解受保护成员的继承和可见性方面存在问题。
我知道它在同一个包和子类中可见。
但在下面的代码中,它在子类中是不可见的。
A.java
package a;
public class A {
public static void main(String[] args) {
}
protected void run() {
}
}
B.java
package b;
import a.A;
public class B extends A {
public static void main(String[] args) {
B b = new B();
b.run(); // this works fine
}
}
C.java
package b;
import a.A;
public class C extends A{ // it will not work also if extends B
public static void main(String[] args) {
B b = new B();
b.run(); // this is the problem; not visible
}
}
为什么最后一个类中的 b.run() 是不可见的?
【问题讨论】:
-
run()的访问修饰符是什么?
标签: java inheritance protected