Make java methods visible to only specific classes

后端 未结 3 773
一个人的身影
一个人的身影 2020-12-11 08:19

I have a manager class that is responsible for managing Objects of a certain kind. To do so it needs to manipulate these Objects, but these Objects have no relation to the m

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-11 09:04

    You're asking for something akin to the "friend" declarations of C++, but there's no direct equivalent in Java - package visibility is the nearest. Alternatively you could go for a model like the XML DOM, where the methods that should be public are defined in interfaces and all client access is via these interfaces. The manager would know the concrete class of the implementation so could downcast to that as required.

提交回复
热议问题