Java -如何定义通用的接口方法。

时间:2022-01-22 09:43:08

I created interface:

我创建的接口:

public interface SubApp {
           public String talk(Result<? extends Object> result);
}

And this implementation:

这实现:

class A implements SubApp{
   @Override
   public String talk(Result<String> result) {...}
}

My IDE gives following error: Method does not override method from its superclass.

我的IDE给出了以下错误:方法不会覆盖来自它的超类的方法。

How do I define method in interface to make method public String talk(Result<String> result) override method of interface?

如何在接口中定义方法使方法公共字符串对话(结果 Result)覆盖接口方法?

4 个解决方案

#1


3  

The issue with your approach is that your implementation is more specific than the interface definition, and thus your method won't accept the breadth of input your interface will, and that's not allowed. (You can refine output types to be more specific, because an output type can be downward cast to the less specific type, but only the opposite is true for inputs).

您的方法的问题是,您的实现比接口定义更具体,因此您的方法不会接受接口的宽度,这是不允许的。(您可以将输出类型细化为更具体的类型,因为输出类型可以向下转换为较不特定的类型,但只有对输入的输出类型是正确的)。

Something like this should work:

像这样的东西应该可以:

public interface SubApp<T extends Object> {
    public String talk(Result<T> result);
}

class A implements SubApp<String> {
    @Override
    public String talk(Result<String> result) { ... }
}

#2


1  

Try something like this -

试试这个吧。

public interface SubApp<T> {
  public String talk(Result<T> result);
}

class A implements SubApp<String> {
  @Override
  public String talk(Result<String> result) {...}
}

#3


1  

Try something like

试着像

public interface SubApp<T> {
   public String talk(Result<T> result);
}

btw, you never need ? extends Object since everything extends object, you are not liming anything with that expression

顺便问一句,你不需要吗?扩展对象,因为所有东西都扩展了对象,你不会用那个表达式来做任何事情。

#4


0  

? represents a wild-card but not a generic. Try the following.

吗?表示一个通配符,但不是一般的。试试下面的。

public interface SubApp<X extends Object>
{
     public String talk(Result<X> result);
}

#1


3  

The issue with your approach is that your implementation is more specific than the interface definition, and thus your method won't accept the breadth of input your interface will, and that's not allowed. (You can refine output types to be more specific, because an output type can be downward cast to the less specific type, but only the opposite is true for inputs).

您的方法的问题是,您的实现比接口定义更具体,因此您的方法不会接受接口的宽度,这是不允许的。(您可以将输出类型细化为更具体的类型,因为输出类型可以向下转换为较不特定的类型,但只有对输入的输出类型是正确的)。

Something like this should work:

像这样的东西应该可以:

public interface SubApp<T extends Object> {
    public String talk(Result<T> result);
}

class A implements SubApp<String> {
    @Override
    public String talk(Result<String> result) { ... }
}

#2


1  

Try something like this -

试试这个吧。

public interface SubApp<T> {
  public String talk(Result<T> result);
}

class A implements SubApp<String> {
  @Override
  public String talk(Result<String> result) {...}
}

#3


1  

Try something like

试着像

public interface SubApp<T> {
   public String talk(Result<T> result);
}

btw, you never need ? extends Object since everything extends object, you are not liming anything with that expression

顺便问一句,你不需要吗?扩展对象,因为所有东西都扩展了对象,你不会用那个表达式来做任何事情。

#4


0  

? represents a wild-card but not a generic. Try the following.

吗?表示一个通配符,但不是一般的。试试下面的。

public interface SubApp<X extends Object>
{
     public String talk(Result<X> result);
}