简体   繁体   English

Java有什么办法可以使方法只能由类和子类使用?

[英]Java any way to keep a method usable only by class and subclasses?

I am fully aware of how public , protected and private in Java work. 我完全了解Java中publicprotectedprivate的工作方式。 I was wondering if there is a way to mimic a modifier in between private and protected. 我想知道是否可以在私有和受保护之间模拟修饰符。 I want only a class and its children to be able to access its children. 我只希望一个班级及其子级能够访问其子级。

I want to do this because I have a class, Savable that stores an int databaseID . 我想这样做是因为我有一个Savable类,它存储一个int databaseID Savable has a save() method that stores the DBID then calls protected abstract ContentValues getSaveData () to obtain any other information from its children to write into the DB. Savable有一个save()方法,该方法存储DBID,然后调用protected abstract ContentValues getSaveData ()来从其子级获取任何其他信息以写入DB。 This however, is unsafe design. 但是,这是不安全的设计。 ContentValues will store all of the objects information, private or not to be written into the DB. ContentValues将存储所有对象信息(私有的或不写入数据库的信息)。

Now, while I could just make save an abstract method in Savable , I would prefer to keep the structure I have now, as I will be adding data to the Savable class later on, and I would prefer to never have to expose this data to the user. 现在,尽管我可以在Savable保存一个抽象方法,但我还是希望保留现在的结构,因为稍后我将数据添加到Savable类中,并且我希望永远不必将数据公开给用户。

So, is there any way to mimic a children-only modifier in Java? 那么,有什么方法可以模仿Java中仅用于儿童的修饰符? If not, I may store the encrypted value of all the data the user supplies in getSaveData() . 如果没有,我可以将用户提供的所有数据的加密值存储在getSaveData()

Requested code: Removed as it is no longer necessary. 要求的代码:已删除,因为不再需要。

So, is there any way to mimic a children-only modifier in Java? 那么,有什么方法可以模仿Java中仅用于儿童的修饰符?

Nope. 不。 The only access modifiers we've got are the ones you listed. 我们仅有的访问修饰符是您列出的那些。 There's also "default" visibility, aka "package-private", but that does not make elements accessible to subclasses. 还有“默认”可见性,也就是“ package-private”,但这不能使元素可用于子类。 So you could probably just use protected and not worry about it. 因此,您可能只使用protected而不必担心。

It sounds like a better solution might be a refactor of your code, but there's not much concrete in your question to go on. 听起来更好的解决方案可能是对代码进行重构,但是您的问题没有什么具体的。

我能想到的唯一方法是将方法设置为protected ,并将类放入其自己的包中。

If you yourself are writing the children then you could make them final . 如果你自己在写孩子,那么你可以使他们final

Mixing this up with the protected modifier would give you something like what you want. 将其与protected修饰符混合在一起,将为您提供所需的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中定义一个接口方法来接受一个类或其任何子类? - How to define an interface method to accept a class or any of its subclasses in Java? 一种Java方法,只能由其自己的类或其他子类调用 - A Java method that can only be called by its own class or by other subclasses Java中有没有办法交出HashSet <Class> 方法的参数,以便该方法将接受Class子类的HashSets? - Is there a way in Java to hand over HashSet<Class> arguments to a method so that the method will accept HashSets of subclasses of Class? 使用仅在子类中定义的方法 - JAVA - using a method that is only defined in the subclasses - JAVA Java - 基类和子类中的equals方法 - Java - equals method in base class and in subclasses 如何使一个类的成员只能在任何包的子类中访问? - How to make a member of a class to be accessible only in subclasses in any packages? 有没有办法使对象只能由访问特定方法的第一个线程使用? - Is there a way to make an object usable only by the first thread that accesses a specific method? 是否有任何注释来警告子类不应在Java中隐藏静态方法 - Is there any annotation to warn that subclasses shouldn't hide a static method in Java java NoClassDefFoundError 但类在那里并且可用 - java NoClassDefFoundError but Class is there and Usable 在java中连接子类和类 - Connect subclasses and class in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM