Java泛型方法继承和覆盖规则 [英] Java generic method inheritance and override rules

查看:32
本文介绍了Java泛型方法继承和覆盖规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有泛型方法的抽象类,我想通过用特定类型替换泛型参数来覆盖泛型方法.所以在伪代码中我有以下内容:

I have an abstract class that has a generic method and I want to override the generic method by substituting specific types for the generic parameter. So in pseudo-code I have the following:

public abstract class GetAndParse {
  public SomeClass var;

  public abstract <T extends AnotherClass> void getAndParse(T... args);
}

public class Implementor extends GetAndParse {
  // some field declarations

  // some method declarations

  @Override
  public <SpecificClass> void getAndParse(SpecificClass... args) {
    // method body making use of args
  }
}

但出于某种原因,我不允许这样做?我是在犯某种语法错误还是不允许这种继承和覆盖?具体来说,我收到关于 @Override 的错误,因为 Eclipse IDE 一直提醒我实现 getAndParse.

But for some reason I'm not allowed to do this? Am I making some kind of syntax error or is this kind of inheritance and overriding not allowed? Specifically I'm getting an error about @Override because the eclipse IDE keeps reminding me to implement getAndParse.

这是我希望上述代码工作的方式.在我的代码的其他地方,有一个方法需要实现 GetAndParse 的对象实例,这特别意味着它们有一个我可以使用的 getAndParse 方法.当我对该实例调用 getAndParse 时,编译器会检查我是否以正确的方式使用了 T 的特定实例,特别是 T应该扩展 AnotherClass 并且应该是 SpecificClass.

Here's how I want the above code to work. Somewhere else in my code there is a method that expects instances of objects that implement GetAndParse which specifically means that they have a getAndParse method that I can use. When I call getAndParse on that instance the compiler checks to see whether I have used specific instances of T in the proper way, so in particular T should extend AnotherClass and it should be SpecificClass.

推荐答案

我们这里有两种不同的方法,每个方法都有各自的类型参数.

What we are having here is two different methods with individual type parameters each.

public abstract <T extends AnotherClass> void getAndParse(Args... args);

这是一个带有名为 T 的类型参数的方法,并以 AnotherClass 为边界,这意味着 AnotherClass 的每个子类型都允许作为类型参数.

This is a method with a type parameter named T, and bounded by AnotherClass, meaning each subtype of AnotherClass is allowed as a type parameter.

public <SpecificClass> void getAndParse(Args... args)

这是一个带有名为 SpecificClass 的类型参数的方法,以 Object 为边界(意味着每个类型都允许作为类型参数).你真的想要这个吗?

This is a method with a type parameter named SpecificClass, bounded by Object (meaning each type is allowed as a type parameter). Do you really want this?

类型参数是否在 Args 中使用?我认为问题就在那里.

Is the type parameter used inside Args? I think the problem would be there.

public abstract <T extends AnotherClass> void getAndParse(T... args);

是方法的调用者可以决定他想用哪个类型参数来调用该方法,只要这是AnotherClass的某个子类型.这意味着实际上可以使用 AnotherClass 类型的任何对象调用该方法.

is that the caller of the method can decide with which type parameter he wants to call the method, as long as this is some subtype of AnotherClass. This means that in effect the method can be called with any objects of type AnotherClass.

由于调用者可以决定类型参数,您不能在子类中将参数类型缩小到 SpecificClass - 这不是方法的实现,而是另一个同名的方法(超载).

Since the caller can decide the type parameter, you can't in a subclass narrow down the parameter type to SpecificClass - this would not be an implementation of the method, but another method with same name (overloading).

也许你想要这样的东西:

Maybe you want something like this:

public abstract class GetAndParse<T extends AnotherClass> {
  public SomeClass var;

  public abstract void getAndParse(T... args);
}

public class Implementor extends GetAndParse<SpecificClass> {
  // some field declarations

  // some method declarations

  @Override
  public void getAndParse(SpecificClass... args) {
    // method body making use of args
  }
}

现在 getAndParse 方法实现了父类的方法.

Now the getAndParse method implements the parent class' method.

这篇关于Java泛型方法继承和覆盖规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆