为什么我不能直接在 subclass 中使用 Reentrant class 的 getOwner() 方法?

Why am i not able to use the getOwner() method of Reentrant class in subclass directly?

我正在尝试使用 class 中的 getOwner 方法,即子class ReentractLock class, 我知道在包之外,受保护的方法将仅对 subclass 可用。 所以我希望 getOwner 方法可用于我的 sublcass MyLock。 但是我无法使用它。

1.

public class Mylock extends ReentrantLock { }

当我使用 new Mylock().getOwner() 我收到错误消息,未定义 getOwner。

2.

public class Mylock extends ReentrantLock {
 String getOwner() {  
 Thread t =  this.getOwner();  
 return t.getName();  
}}

现在当我使用新的 MyLock().getOwner() 时它可以工作。

我的问题是当我将 new MyLock().getOwner() 与第一个逻辑一起使用时,为什么它显示方法未定义,至少我应该获得线程对象。

According to the documentation ReentrantLock.getOwner()protected。当您重写该方法时,您实际上是在授予它默认访问权限。有关详细信息,请阅读 publicprotected、默认和 private 访问级别之间的差异。