我在下面的代码与Eclipse IDE中写道:如何在无关的类中访问受保护的方法?
public interface X
{
final public static int SOME_CONST = 0;
}
public class Handle implements X
{
protected void methodHandle() { }
//...
}
public class User implements X
{
Handle handle = new Handle();
private void methodUser()
{
Y y = new Y() // anonymous inner class
{
public void methodY()
{
handle.methodHandle(); // <--- why this is NOT giving error ?
}
}
}
}
即使Handle.methodHandle()
为protected
,它仍然从调用一个匿名内部class
方法的内部方法?为什么会发生,我错过了什么? Handle
和User
之间的唯一关系是它们是implement
相同的X
。
+1是的,他们确实不是“不相关的”。 – iammilind