简体   繁体   English

如何在Java中实现接口的类中添加受保护的方法?

[英]How can I add a protected method to a class implementing an interface in java?

I have a class called Property which has nothing but get -methods. 我有一个叫做Property的类,除了get方法外别无其他。 All the fields will be set when a new instance of Property is created. 创建Property的新实例时将设置所有字段。 Property implements an interface called IProperty . Property实现了一个称为IProperty的接口。

Due to some bug in a library I use, I have to set the name of an instance of Property anew after its creation. 由于我使用的库中存在一些错误,因此在创建Property实例后,必须重新设置其名称。 Therefore it was suggested to create a WrapperProperty class that will provide a public setName -method which itself calls a therefore created setName() -method in Property , which will be protected/package view. 因此,建议创建一个WrapperProperty类,该类将提供一个公共setName方法,该方法本身将在Property调用因此创建的setName()方法,该方法将受到保护/封装。

The problem is that I cannot make this method protected in Property , because Eclipse tells me to add it to the interface IProperty and make it public. 问题是我无法在Property中使此方法受保护,因为Eclipse告诉我将其添加到接口IProperty并将其公开。

Is there some work-around to it? 有一些解决方法吗?

WrapperIProperty: 包装IP属性:

public class WrapperIProperty {

    private IProperty prop;

    WrapperIProperty(Property prop) {
        this.prop = prop;
    }

    public void setName(String name) {
        prop.setName(name);
    }
}

Property: 属性:

public class Property implements IProperty {

    String name;

    protected void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
    public int getFoobar() {
        return 123;
    }
    public int getWhatever() {
        return 987;
    }
}

IProperty: IPProperty:

public interface IProperty {

    public int getWhatever();
    public int getFoobar();
    public String getName();

}

This is how it looks at the moment. 目前情况就是这样。 Obviously it won't work, since I cannot let the method be protected in the Property class. 显然,这是行不通的,因为我无法让该方法在Property类中受到保护。 Therefore I best get rid of the interfacee entry somehow. 因此,我最好以某种方式摆脱接口条目。 But how? 但是如何?

What you probably want to do is to leave the IProperty interface alone (don't add the setName method to it) and create a delegating wrapper class which provides the method you want (wraps an implementation of the interface). 您可能想要做的就是不让IProperty接口(不要向其中添加setName方法),而是创建一个提供所需方法的委托包装器类(包装接口的实现)。

This way you can feed wrapped properties and regular properties to whatever needs them. 这样,您可以将包装的属性和常规属性提供给需要的任何内容。

public class WrappedProperty implements IProperty {

    private String name;

    private Property prop;

    WrappedProperty (Property prop) {
        this.prop = prop;
    }

    protected void setName(String name) {
        this.name = name;
    }
    public int getWhatever() {
        return prop.getWhatever();
    }
    public int getFoobar() {
        return prop.getFoobar();
    }    
    public String getName() {
        if (this.name == null) {
           return prop.getName():
        } else {
            return this.name; 
        }
    }
}
public class Property implements IProperty {        

    public String getName() {
        return "blah";
    }
    public int getFoobar() {
        return 123;
    }
    public int getWhatever() {
        return 987;
    }
}
public interface IProperty {

    public int getWhatever();
    public int getFoobar();
    public String getName();

}

Methods in an Interface are public in scope so implementing class cannot override methods by reducing their accessibility. Interface中的方法在范围上是公共的,因此实现类不能通过减少其可访问性来覆盖方法。 Make them public public

You cannot have a public methodName in an Interface and a private or protected methodName in a Class implementing this Interface. 您不能在接口中具有公共methodName ,而在实现此接口的Class中不能具有私有或受保护的methodName

So you can have the methodName public in your Class : 因此,您可以在您的Class中使用methodName公共:

  • this method do nothing 这种方法什么都不做
  • this method call [another]methodNameProtected (you give another name to a new protected method) 此方法调用[another] methodNameProtected (为新的受保护方法命名)

UPDATE 更新

If you want it only in Interface you have to change your Interface in an AbstractClass and put in it the method public final returnCode methodName if the method is common for all inherited classes 如果只希望在Interface中使用它,则必须在AbstractClass中更改Interface,然后将方法public final returnCode methodName放入其中,如果该方法对于所有继承的类都是通用的

Found the solution to that problem: 找到了解决该问题的方法:

WrapperIProperty : 包装IP属性:

public class WrapperIProperty {

    private Property prop;

    public WrapperIProperty(IProperty prop) {
        this.prop = (Property) prop;
    }

    public void setName(String name) {
        prop.setName(name);
    }

}

Property: 属性:

public class Property implements IProperty {

    private String name = null;

    [...]

    void setName(String name) {
        this.name = name;
    }
}

IProperty: IPProperty:

public interface IProperty {

    [...]
}

This will do the job 这样就可以了

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

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