[英]Same method for multiple classes that implement the same interface
Sorry I really didn't know how to title the question, here's the problem... 对不起,我真的不知道如何标题,这是问题所在......
I have an interface and multiple classes that implement the interface. 我有一个接口和多个实现接口的类。 The implementation of some of the methods in the interface are exactly the same in every implementing class.
接口中某些方法的实现在每个实现类中都是完全相同的。 I feel like there should be a way to simplify this so I don't have to write the same code every time.
我觉得应该有一种简化方法,所以我不必每次都写相同的代码。 Example:
例:
public interface Foo {
String getName();
}
public class FooImpl1 implements Foo {
private String name = "foo name1";
public String getName() {
return name;
}
}
public class FooImpl2 implements Foo {
private String name = "foo name2";
public String getName() {
return name;
}
}
So to break down.. 所以打破..
is there a way to put the code for getName in one place and each class has it's own name variable? 有没有办法将getName的代码放在一个地方,每个类都有自己的名称变量?
is there a way to make getName static so I don't have to create a new instance 有没有办法使getName静态,所以我不必创建一个新的实例
Have better ideas? 有更好的想法吗?
Use an abstract class and initialize the field in the constructor: 使用抽象类并初始化构造函数中的字段:
public abstract class Foo {
protected String name;
String getName() {
return name;
}
}
public class FooImpl1 extends Foo {
public FooImpl1 () {
name = "foo name1";
}
}
public class FooImpl2 extends Foo {
public FooImpl1 () {
name = "foo name2";
}
}
JB Nizlet pointed out that it would be cleaner to do something like this: JB Nizlet指出,做这样的事情会更干净:
public abstract class Foo {
protected String name;
public Foo(String newName) {
name = newName;
}
String getName() {
return name;
}
}
And then call super("foo name 1")
in the subclasses. 然后在子类中调用
super("foo name 1")
。
As @Peter Lawrey said, if you already have an interface or want one for some other reason: 正如@Peter Lawrey所说,如果您已经有一个界面或者出于其他原因需要一个界面:
public interface IFoo {
String getName();
}
public abstract class Foo implements IFoo {
protected String name;
String getName() {
return name;
}
}
You could take a very simple approach: 你可以采取一个非常简单的方法:
public interface Foo {
String getName();
}
public class Named implements Foo {
String name;
public String getName() {
return name;
}
}
public class FooImpl1 extends Named {
{name = "foo name1";}
}
public class FooImpl2 extends Named {
{name = "foo name2";}
}
It doesn't really work for getters like in your example, but since the question is more generic I think it's worth mentioning that with Java 8 you can define default methods in interfaces . 它并不像你的例子那样适用于getter,但由于问题更通用,我认为值得一提的是,使用Java 8,你可以在接口中定义默认方法 。 So if you had something a bit more complex like:
所以如果你有一些更复杂的东西,比如:
public interface ComplexFoo {
String getFirstName();
String getLastName();
String getFullName();
}
You do not have to repeat the getFullName
implementation in every subclass, but can specify it in the interface: 您不必在每个子类中重复
getFullName
实现,但可以在接口中指定它:
public interface ComplexFoo {
String getFirstName();
String getLastName();
default String getFullName() {
return getFirstName() + " " + getLastName();
}
}
The getters though still need repeating, since they access members that are not yet available in the interface: 虽然getter仍然需要重复,因为它们访问界面中尚未提供的成员:
public class FooImpl1 implements ComplexFoo {
private String firstName = "foo";
private String lastName = "name1;
private String getFirstName() { return firstName; }
private String getLastName() { return lastName; }
}
public class FooImpl2 implements ComplexFoo {
private String firstName = "foo";
private String lastName = "name2;
private String getFirstName() { return firstName; }
private String getLastName() { return lastName; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.