Is there a clean way of defining a Java interface method that has different parameters for each implementation class?

huangapple go评论101阅读模式
英文:

Is there a clean way of defining a Java interface method that has different parameters for each implementation class?

问题

例如,假设我有一个定义了方法action(...)的接口,然后有三个(实际情况中可能更多)类分别实现了具有不同参数的action方法,如下所示:

public class Class1 implements SomeInterface {
    public int action(int num) { ... }
}

public class Class2 implements SomeInterface {
    public String action(String string) { ... }
}

public class Class3 implements SomeInterface {
    public double action(double dub) { ... }
}

我觉得这似乎不太可能,但肯定有一些方法可以告诉用户每个类都有一个action方法;也许有一种方法可以动态地接受参数?

编辑: 如果我们不知道确切的参数数量怎么办?例如,类似这样的写法是否可行?

interface SomeInterface<T, T, ..., T> {
    T action(T t1, S s, ..., U u);
}

我感觉这是一个非常奇怪的问题,所以我会提供更多的背景信息。我正在开发一个卡牌游戏,其中有不同的行动卡,每张行动卡都有不同的功能,并且需要不同的信息。我认为在这种情况下需要一个行动卡接口,但后来我遇到了一个问题,即如何在每个实现类需要完全不同的参数和不同的参数数量时指定action方法,而不是它们以不同的方式实现相同的方法,或者它们每个都需要相同数量的参数。因此,我理解在这里泛型可能不适用,但也许我错了,或者有更好的方法来解决这个问题;也许从一开始就使用接口可能不是正确的方法?

英文:

For example, say I have an interface that defines the method action(...) and I have three (in reality way more) classes that implement action with different parameters like so:

public class Class1 implements SomeInterface {
    public int action(int num) { ... }
}

public class Class2 implements SomeInterface {
    public String action(String string) { ... }
}

public class Class3 implements SomeInterface {
    public double action(double dub) { ... }
}

I feel like this isn't possible, but there's got to be some way to tell the user that each of these classes have an action method; maybe there's a way for it to dynamically accept arguments?

EDIT: What if we don't know that there is exactly one parameter? For example, is something like this possible?

interface SomeInterface&lt;T, T, ..., T&gt; {
    T action(T t1, S s, ..., U u);
}

I feel like this is a really strange question, so I'll try to provide some more context. I'm working on a card game where there are different Action Cards that do different things, and require different information. I thought an Action Card interface was in order here, but then I ran into the issue of how to specify action when each implementation class needs totally different parameters and a different number of parameters as opposed to them implementing the exact same method differently or them each requiring the same number of parameters. For this reason, it is my understanding that generics won't work here but maybe I'm wrong or there's a better way of doing this altogether; perhaps the interface was the wrong way to do it from the start?

答案1

得分: 4

以下是翻译好的内容:

你可以使用 泛型

interface SomeInterface<T> {
    T action(T t);
}

class Class1 implements SomeInterface<Integer> {
    public Integer action(Integer i) { return null; }
}

class Class2 implements SomeInterface<String> {
    public String action(String s){ return null; }
}

class Class3 implements SomeInterface<Double> {
    public Double action(Double d) { return null; }
}

泛型只能用于对象类型,所以你必须使用原始类型的包装类而不是基本数据类型。

英文:

You can use generics:

interface SomeInterface&lt;T&gt; {
    T action(T t);
}

class Class1 implements SomeInterface&lt;Integer&gt; {
    public Integer action(Integer i) { return null; }
}

class Class2 implements SomeInterface&lt;String&gt; {
    public String action(String s){ return null; }
}

class Class3 implements SomeInterface&lt;Double&gt; {
    public Double action(Double d) { return null; }
}

Generics can only be used for object types, so you have to use primitive wrappers instead of primitives.

huangapple
  • 本文由 发表于 2020年8月20日 10:55:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63497646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定