而从泛型类类型/形式类型参数T/E
子类化具有有效的类类型/实际类型参数,例如Type/String
有很多组合发生,这令人困惑,什么时候使用哪一个?
public class SubClass<T> implements SuperIfc<T> <-- It is straight forward to understand
public class SubClass<T> implements SuperIfc<Type>
public class SubClass<Type> implements SuperIfc<T>
public class SubClass<Type> implements SuperIfc<Type>
public class SubClass<Type> implements SuperIfc
public class SubClass implements SuperIfc<Type>
public class SubClass implements SuperIfc<T> <--- Hope we cannot declare <T> in his case while initialising SubClass.
// Bounded type parameter
public class SubClass<T extends Type> implements SuperIfc<Type>
public class SubClass<T extends Type> implements SuperIfc<T> <-- Looks <T> at SuperIfc also refers <T extends Type>, and no need to declare it again at SuperIfc.
// Recursive type bound
public class SubClass<T extends Comparable<T>>> implements SuperIfc<T>
public class SubClass<T extends Comparable<T>>> implements SuperIfc<Type>
这样我可以更清楚地解决incompatible types while subclassing
情况1:public class Test {
interface TestIfc {
public static <T extends TestIfc> T of(int choice) {
if(choice == 1) {
return new TestImpl(); <-- PROB_1: incompatible type error
} else {
return new SomeOtherTestImpl(); //incompatible type error
}
}
}
static class TestImpl implements TestIfc {}
static class SomeOtherTestImpl<T extends TestIfc> implements TestIfc {
//The below method also having same error though with declaration
public T of() {
return new TestImpl(); <-- PROB_2: incompatible type error
}
}
}
案例_1:PROB_1:返回类型为T extends TestIfc
并返回 TestImpl implements TestIf
那么有什么问题呢?Case_1:PROB_2:与PROB_1类似,如何在没有外部类型转换的情况下进行纠正。请帮忙。
案例_2:
public interface SuperIfc<T> {
public T create(Object label);
}
class Type {
public static Type of(){
return new Type();
}
}
------
public class SubClass<Type> implements SuperIfc<Type>{
@Override
public Type create() {
return Type.of(); <---- PROB_1: cannot resolve method
}
}
-------
public class SubClass<T extends Type> implements SuperIfc<Type>{
@Override
public Type create() {
return Type.of(); <---- PROB_1: is resolved
}
}
SuperIfc<Type> object = new SubClass(); <-- PROB_2 Unchecked assignement warning
SuperIfc<Type> object = new SubClass<TypeImpl>(); <-- PROB_3: bound should extend Type
T
上课Type
子类化时?可能是下面和何时使用之间的区别? public class SubClass<Type> implements SuperIfc<Type>
public class SubClass<Type> implements SuperIfc
public class SubClass implements SuperIfc<Type>
public class SubClass<T extends Type> implements SuperIfc<Type>
public class SubClass<T extends Type> implements SuperIfc<T>
public class SubClass<T> implements SuperIfc<Type>
最佳答案
在第一个 of()
方法,该方法可以返回任何实现 InformationIfc
的类型,但您的方法总是返回一个特定的实现 - InformationImpl
- 这是 Not Acceptable 。
例如,如果您有其他类(class) SomeOtherInformationImpl
实现该接口(interface),该方法的调用者将被允许编写:
SomeOtherInformationImpl i = InformationImpl.of();
但是您的方法没有返回
SomeOtherInformationImpl
.第二个
of()
方法与第一种方法具有相同的问题。如果您使用以下方法实例化您的类:
InformationImpl i = new InformationImpl<SomeOtherInformationImpl>();
of()
方法必须返回 SomeOtherInformationImpl
,而不是 InformationImpl
.
关于子类化时Java泛型不兼容的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61789583/