我在下面的代碼與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