Java泛型方法继承和覆盖规则 [英] Java generic method inheritance and override rules
问题描述
我有一个抽象类,它有一个泛型方法,我想通过用特定类型替换泛型参数来覆盖泛型方法。所以在伪代码中我有以下内容:
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)
这是一个名为<$ c的类型参数的方法$ c> 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屋!