我如何在接口中重载方法? [英] How would I overload method in an interface?
问题描述
如果我有这个界面
public interface someInterface {
// method 1
public String getValue(String arg1);
// method 2
public String getValue(String arg1, String arg2);
}
我希望能够将1或2个字符串传递给getValue方法不必在每个实现类中都覆盖它们。
I want to be able to pass in 1 or 2 string to the getValue method without having to override both in each implementing class.
public class SomeClass1 impelments someInterface
{
@Override
public String getValue(String arg1);
}
public class SomeClass2 implements someInterface
{
@Override
public String getValue(String arg1, String arg2);
}
这不起作用,因为SomeClass1需要实现方法2而SomeClass2需要实施方法1.
this won't work because SomeClass1 needs to implement method 2 and SomeClass2 needs to implement method 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");
有没有更好的方法呢?
推荐答案
接口用作用户的合同该接口:指定可用的方法(在所有实现中)以及如何调用它们。如果接口的两个实现需要不同的方法,那么该方法 not 成为界面的一部分:
An interface serves as a contract for the users of that interface: you specify what methods are available (in all implementations) and how they are called. If two implementations of an interface need a different method, then that method should not be part of the interface:
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) {
// ...
}
}
在您的程序中,您将知道您使用的是哪种实现,因此您只需调用正确的函数:
In your program, you will know which implementation you use, so you can simply call the right function:
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));
}
}
替代方法
如果您的类 Program
更通用并使用依赖注入,您可能不知道您拥有哪种实现。然后,我会创建一个新接口 Key
,它可以是键的类型:
If your class Program
is more generic and uses dependency injections, you may not know which implementation you have. Then, I would make a new interface Key
, which can be either type of 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;
// ...
}
程序中的依赖注入可能来自一些工厂的实施。由于您无法知道您使用哪种类型的查询,因此您需要一份 getValue
的合约。
The dependency injection in your program might come from some factory implementation. Since you cannot know which type of lookup you use, you need a single contract for 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()));
}
}
这篇关于我如何在接口中重载方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!