简体   繁体   English

Java:公开受保护方法的简便方法

[英]Java: Easy way to make protected methods public

I have a class (Capsule) which has a lot of methods (30+) that are protected. 我有一个类(胶囊),其中有很多受保护的方法(超过30种)。 The idea is to allow devs to extend this class and use the protected methods within the class (ImADev), but leave it up to the dev to expose them as public (Overriding the methods as they see fit). 这个想法是允许开发人员扩展该类并在该类(ImADev)中使用受保护的方法,但要让开发人员将它们公开给公众(按他们认为合适的方法覆盖)。

I now have a use case where I want to pass these objects into a few Utility classes/methods and allow them to have access to the protected methods .. except they're protected, so my utility classes/services can't see them. 现在,我有一个用例,我想将这些对象传递给一些实用程序类/方法,并允许它们访问受保护的方法..除了它们是受保护的之外,因此我的实用程序类/服务无法看到它们。

See the example below: 请参阅以下示例:

// My Capsule class with protected methods
public abstract class Capsule {
   public Capsule(..) { .. }
   protected String getFoo() { return "foo"; }
}

// How a dev should use the class
public class ImADev extends Capsule {
    public ImADev(..) { super(..); }
    public String getBar() { this.getFoo() + "BAR");
}

// A special utility class
// Helper.helper() can accept any class derived from Capsule
public class Helper {
    public String helper(Capsule c) { 
        return " im doing something awesome with " + c.getFoo();
        // Except i cant do this, because c.getFoo() is protected.
    } 
}

What's the best way around this? 最好的办法是什么? Keep in mind I have a lot of protected methods. 请记住,我有很多受保护的方法。

Either your class is doing too much, in which case break it up into smaller more well defined classes 您的课程做得太多,在这种情况下,将其分解为更小,定义更明确的课程

Or, implement safe public methods that your utility class can call to wrap you protected methods 或者,实现实用程序类可以调用的安全公共方法来包装受保护的方法

Or, just make them public because it sounds like they might/should be 或者,只是将它们公开,因为听起来好像他们可能/应该

Or, separate the data in the class from the behaviour (composition) then your utility class only has to deal with safer public getter methods an not the protected behaviour methods that can change the internals 或者,将类中的数据与行为(组成)分开,那么您的实用工具类仅需要处理更安全的公共获取方法,而无需处理可以更改内部结构的受保护行为方法。

A bit difficult to provide a better answer without knowing more about your application and it's use 在不了解您的应用程序及其使用情况的情况下,很难提供更好的答案

There is no way 'around' this, except to put the utility class in the same package as the classes with the protected method. 除了将实用程序类与具有受保护方法的类放在同一包中之外,没有其他方法可以解决。 If those classes are in more then one package, that won't help. 如果这些类包含在多个软件包中,那将无济于事。

In deciding to use protected, you made a decision, and now you are stuck with the implications. 在决定使用protected时,您已经做出了决定,而现在您被困在其中了。 You can live with it, change to public, or change package membership to take advantage of default access. 您可以使用它,更改为公共名称或更改包成员身份以利用默认访问权限。

I see at least two options. 我至少看到两个选择。

  1. Make Helper a subclass of Capsule. 将Helper设为Capsule的子类。 Or, 要么,
  2. Turn your protected methods into default accessibility, and put the Helper class in the same package as the Capsule class. 将您的受保护方法转换为默认可访问性,然后将Helper类与Capsule类放在同一包中。

Edit: You can also probably accomplish exactly what you want with reflection. 编辑:您也可以通过反射完全完成您想要的操作。

Actually there is a way to call protected methods from outside the package or subclass: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/reflect/AccessibleObject.html 实际上,有一种方法可以从包或子类外部调用受保护的方法: http : //docs.oracle.com/javase/1.5.0/docs/api/java/lang/reflect/AccessibleObject.html

I'm just kidding! 我只是在开玩笑! This tool exists for technical stuff like serialization of persistance. 该工具用于持久化序列化之类的技术工作。

Declaring a method as "protected" is just a way to organise your code and define which part should deal which which data, so trying to get "around" is not a good idea, unless you mistakenly declared it protected. 将方法声明为“保护”只是一种组织代码并定义哪个部分应该处理哪些数据的方法,因此,除非您错误地将其声明为受保护,否则尝试“绕过”并不是一个好主意。 In this case correct the first mistake. 在这种情况下,请纠正第一个错误。

If you are interested in how to organize your code in an efficient and clear way, i highly recommend reading http://java.sun.com/docs/books/effective/ 如果您对如何高效,清晰地组织代码感兴趣,我强烈建议您阅读http://java.sun.com/docs/books/effective/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM