简体   繁体   English

实现相同接口的多个类的方法相同

[英]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.. 所以打破..

  1. is there a way to put the code for getName in one place and each class has it's own name variable? 有没有办法将getName的代码放在一个地方,每个类都有自己的名称变量?

  2. 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.

相关问题 在 Java 中的同一个接口中实现多个类? - Implement multiple classes in the same interface in Java? 如何实例化(在方法内部)实现同一接口的不同类? - How to instantiate (inside a method) different classes that implement the same interface? 在Java中,如何区分实现相同接口的类? - In Java, how to distinguish classes that implement the same interface? 在实现相同接口的类之间切换 - Switch between classes which implement a same interface 实现相同接口的类之间的关系 - Relationships between classes that implement same interface 为什么实现接口的类不算与Java中的接口类型相同? - Why do classes that implement an interface not count as the same type as the interface in Java? 实现接口以调用相同方法的类数组 - Array of classes implementing an interface to call same method 转换实现相同接口的类的快速方法 - Quick method to convert classes implementing same interface 如何传递多个实现相同接口的类型? - How to pass multiple Types that implement the same interface? 如何创建两个具有相同名称,签名和返回类型的方法的类,就像它们实现相同的接口一样 - How to make two classes which both have method with the same name, signature and return type behave like they would implement the same interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM