我如何在接口中重载方法?

cvu*_*vue 8 java oop interface

如果我有这个界面

public interface someInterface {
  // method 1
  public String getValue(String arg1);
  // method 2
  public String getValue(String arg1, String arg2);
}
Run Code Online (Sandbox Code Playgroud)

我希望能够将1或2个字符串传递给getValue方法,而不必在每个实现类中都覆盖它们.

public class SomeClass1 impelments someInterface 
{
 @Override
 public String getValue(String arg1);
}

public class SomeClass2 implements someInterface 
{
 @Override
 public String getValue(String arg1, String arg2);
}
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为SomeClass1需要实现方法2而SomeClass2需要实现方法1.

我坚持这样做吗?

public interface someInterface2 {
  public String getValue(String... args);
}

public class SomeClass3 implements someInterface2 
{
  @Override
  public String getValue(String... args) {
    if (args.length != 1) {
      throw IllegalArgumentException();
    }
    // code
  }
}

public class SomeClass4 implements someInterface2
{
  @Override
  public String getValue(String... args) {
    if (args.length != 2) {
      throw IllegalArgumentException();
     }
    // code
  }
}

someInterface2 someClass3 = new SomeClass3();
someInterface2 someClass4 = new SomeClass4();
String test1 = someClass3.getValue("String 1");
String test2 = someClass4.getValue("String 1, "String 2");
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法呢?

Vin*_*ele 11

接口用作该接口用户的契约:指定可用的方法(在所有实现中)以及如何调用它们.如果接口的两个实现需要一种不同的方法,则该方法应该是接口的一部分:

public interface Lookup {
}

public class MapLookup implements Lookup {
    public String getValue(String key) {
        //...
    }
}

public class GuavaLookup implements Lookup {
    public String getValue(String row, String column) {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的程序中,您将知道您使用的实现,因此您只需调用正确的函数:

public class Program {
    private Lookup lookup = new MapLookup();

    public void printLookup(String key) {
        // I hardcoded lookup to be of type MapLookup, so I can cast:
        System.out.println(((MapLookup)lookup).getValue(key));
    }
}
Run Code Online (Sandbox Code Playgroud)

替代方法

如果您的类Program更通用并使用依赖注入,您可能不知道您拥有哪个实现.然后,我会创建一个新的接口Key,它可以是任何一种键:

public interface Lookup {
    // ...

    public String getValue(Key key);
}

public interface Key {
}

public MapKey implements Key {
    private String key;
    // ...
}

public GuavaKey implements Key {
    private String row, column;
    // ...
}
Run Code Online (Sandbox Code Playgroud)

程序中的依赖注入可能来自某些工厂实现.由于您无法知道您使用哪种类型的查询,因此您需要一份合同getValue.

public interface Factory {
    public Lookup getLookup();
    public Key getKey();
}

public class Program {
    private Lookup lookup;

    public Program(Factory factory) {
        lookup = factory.getLookup();
    }

    public void printLookup(Factory factory) {      
        System.out.println((lookup.getValue(factory.getKey()));
    }
}
Run Code Online (Sandbox Code Playgroud)


sma*_*c89 10

从 Java 8 开始,您可以通过使用default关键字让接口提供方法的实现。因此,一个新的解决方案是提供这两种方法的默认实现,这可能会引发异常,然后从默认接口派生实际实现。

无论如何,这里是你如何做到这一点:

public interface SomeInterface {
    // method 1
    default String getValue(String arg1) {
        // you decide what happens with this default implementation
    }

    // method 2
    default String getValue(String arg1, String arg2) {
        // you decide what happens with this default implementation
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,让类覆盖正确的方法

public class SomeClass1 implements SomeInterface {
    @Override
    public String getValue(String arg1) {
        return arg1;
    }
}

public class SomeClass2 implements SomeInterface {
    @Override
    public String getValue(String arg1, String arg2) {
        return arg1 + " " + arg2;
    }
}
Run Code Online (Sandbox Code Playgroud)