Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java重写接口为param的抽象类方法_Java_Oop - Fatal编程技术网

Java重写接口为param的抽象类方法

Java重写接口为param的抽象类方法,java,oop,Java,Oop,我有一个抽象类: public abstract class Foo{ //some attributes public Foo(params){} public abstract SomeInterface search(Long id); public abstract boolean insert(SomeInterface param); } 我有以下课程: public class InterfaceImplementation implements So

我有一个抽象类:

public abstract class Foo{
   //some attributes

   public Foo(params){}

   public abstract SomeInterface search(Long id);
   public abstract boolean insert(SomeInterface param);
}
我有以下课程:

public class InterfaceImplementation implements SomeInterface {
  //Some code here
}
然后我创建一个Foo实例:

public class Bar extends Foo{

  public Foo(params){
    super(params);
  }

  @Override
  public InterfaceImplementation search(Long id){
    // some code here
  }
  // The above code compiles

  @Override
  public boolean insert(InterfaceImplementation param){
    // some code specific to the InterfaceImplementation here
  }
  // This function does not compile/
}
那么,我做错了什么?我怎样才能实现我想要做的呢?

您的类栏不会覆盖insert,因为参数类型必须完全匹配。这是因为您的实现采用的是InterfaceImplementation,而不是SomeInterface

您可以执行哪些操作来编译此文件:

在Bar的insert方法中将SomeInterface作为参数,因为任何子类都应该能够处理由Foo指定的SomeInterface的任何实现。 或

在SomeInterface中引入泛型以指定参数类型

public abstract class Foo<T extends SomeInterface>{
然后可以指定子类中的T:

public class Bar extends Foo<InterfaceImplementation>{
类栏不会覆盖insert,因为参数类型必须完全匹配。这是因为您的实现采用的是InterfaceImplementation,而不是SomeInterface

您可以执行哪些操作来编译此文件:

在Bar的insert方法中将SomeInterface作为参数,因为任何子类都应该能够处理由Foo指定的SomeInterface的任何实现。 或

在SomeInterface中引入泛型以指定参数类型

public abstract class Foo<T extends SomeInterface>{
然后可以指定子类中的T:

public class Bar extends Foo<InterfaceImplementation>{
假设我有

Foo foo = new Bar();
并且试着去做

foo.insert(new OtherInterfaceImpl());
我可以这样做,因为Fooinsert接受SomeInterface,而OtherInterfaceImpl实现SomeInterface

然而,Bar的insert实现无法接受OtherInterfaceImpl,因为您在声明中明确表示了这一点

 public boolean insert(InterfaceImplementation param){
这将破坏类型安全,因此不允许

您可以使用如图所示的泛型。

假设我有

Foo foo = new Bar();
并且试着去做

foo.insert(new OtherInterfaceImpl());
我可以这样做,因为Fooinsert接受SomeInterface,而OtherInterfaceImpl实现SomeInterface

然而,Bar的insert实现无法接受OtherInterfaceImpl,因为您在声明中明确表示了这一点

 public boolean insert(InterfaceImplementation param){
这将破坏类型安全,因此不允许

如图所示,您可以使用泛型。

在实现超类方法时,您不能限制参数

可以将输出限制为子类型

以这个方法为例

Number sum(Number a, Number b);
可与任意数字一起使用,例如

Number out = sum(Double.valueOf(1.23), Integer.valueOf(7));
候选人呢

Double sum(Number a, Number b) {
  return a.doubleValue() + b.doubleValue()
}
vs

两种方法中哪一种正确实现了上述API?

在实现超类方法时,不能限制参数

可以将输出限制为子类型

以这个方法为例

Number sum(Number a, Number b);
可与任意数字一起使用,例如

Number out = sum(Double.valueOf(1.23), Integer.valueOf(7));
候选人呢

Double sum(Number a, Number b) {
  return a.doubleValue() + b.doubleValue()
}
vs


这两个API中哪一个正确实现了上述API?

为什么要使用InterfaceImplementation而不是Bar中的某个Interface?它不应该关心哪个实现,只是它是一个SomeInterface,因为我需要编写一些特定于该实现的代码。我也尝试过抽象类,结果也是一样的。insert操作会将对象插入数据库,我需要根据该对象的参数在数据库上设置参数。也许我对这个问题采取了错误的方法?你真的需要允许一个酒吧被视为一个Foo,Foo是根据接口定义的,而不是实际的类吗?我一直这样做,但我觉得不太舒服。我会按照@rgetman的建议将所有内容更改为泛型。为什么您使用InterfaceImplementation而不是Bar中的某个Interface?它不应该关心哪个实现,只是它是一个SomeInterface,因为我需要编写一些特定于该实现的代码。我也尝试过抽象类,结果也是一样的。insert操作会将对象插入数据库,我需要根据该对象的参数在数据库上设置参数。也许我对这个问题采取了错误的方法?你真的需要允许一个酒吧被视为一个Foo,Foo是根据接口定义的,而不是实际的类吗?我一直这样做,但我觉得不太舒服。我会按照@rgetman的建议将所有内容改为泛型。这太完美了。多谢了,这太完美了。谢谢