[英]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公共:
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.