使java方法仅对特定类可见 [英] Make java methods visible to only specific classes

查看:168
本文介绍了使java方法仅对特定类可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个经理类,负责管理某种对象。为此,它需要操纵这些对象,但这些对象与管理器无关,因此从技术上讲,它们位于单独的包project.managers和project.objects中。重要的是,有问题的对象只能由管理者操纵,而不是其他任何地方,但需要项目中的其他每个类都可以访问。

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 manager whatsoever, so design technically, they are in separate packages "project.managers" and "project.objects" . The important thing is that the Objects in question should only be manipulated by the managers and nowhere else, but need to be accessible by every other class in the project.

这样我希望管理员可以访问操作方法,但是限制对其他所有类的访问。最明显的一种方法是将管理器类和对象类移动到同一个包中并声明受保护的操作方法,但由于管理器和对象是完全独立的实体,因此它们不符合哲学。

As such I'd like to have the managers have access to manipulating methods, but restrict access to every other class. The most obvious one would be to move the manager class and object class into the same package and declare manipulating methods protected, but as the managers and objects are completely separate entities they don't fit there philosophically.

(这部分是因为我希望我的IDE在我自动完成有问题的对象上的代码时停止向我显示操作方法,所以我总是必须通过管理器去路由,以便每当相应的表都正确更新我改变了有问题的对象。)

(This is partly because I want my IDE to stop showing me the manipulating methods whenever I autocomplete code on the Objects in question so I always have to go the route through the manager so corresponding tables are correctly updated whenever I change the Objects in question).

对于那个有什么想法,或者在任何情况下最明显的方式是什么?

Are there any ideas to that or is the obvious way the best in any case?

推荐答案

为什么没有名为

ManagerFunctions

另一个名为

ClientFunctions

您管理的对象将实现这两个

You managed objects will implement both of these.

创建托管对象时,可以传递它们,但仅作为对 ClientFunctions 的引用。但是,管理器对象将它们称为 ManagerFunctions ,因此可以访问其托管功能。适当的转换将简单地公开适当的方法。

When you create the managed objects, you pass them around, but only as references to ClientFunctions. The manager objects will, however, refer to them as ManagerFunctions and consequently have access to their 'managed' functions. The appropriate casting will simply expose the appropriate methods.

根据引用这些对象的方式,IDE将自动显示适当的方法。

Your IDE will automatically present you wil the appropriate methods depending on how these objects are referenced.

这篇关于使java方法仅对特定类可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆