java 什么是从另一个对象访问的受保护方法访问级别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6705423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:57:55  来源:igfitidea点击:

What is protected method access level with regard to accessing from another object

java

提问by blue-sky

I'm passing an object reference to a Util class. I'm attempting to invoke a protected method on the Util class but I getting a compile time error -

我将对象引用传递给 Util 类。我试图在 Util 类上调用受保护的方法,但出现编译时错误 -

The method setPositionChild(Field, int, int) from the type Manager is not visible

To invoke a protected method is it required to be in the implementing class only ? Can I not pass the reference to an external class and invoke the reference from there ?

要调用受保护的方法是否只需要在实现类中?我不能将引用传递给外部类并从那里调用引用吗?

回答by Nishant

Protected method can be accessed from subclasses. Make that method public if you want to access from any class. See details here

可以从子类访问受保护的方法。如果您想从任何类访问,请将该方法设为公开。在此处查看详细信息

Also as mentioned by @Sean Patrick Floyd, from classes in the same package!

正如@Sean Patrick Floyd 所提到的,来自同一个包中的类!