从泛型类类型/形式类型参数进行子类化时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
Case_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
}
}
}
Case_1:PROB_1:返回类型是T extends TestIfc
并返回TestImpl implements TestIf
那么有什么问题吗?
Case_1:PROB_2:与PROB_1类似,如何在不进行外部铸造的情况下进行纠正。请帮忙。
Case_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
-
我想知道如何一起解决Case_2、PROB_1和PROB_2?
-
如何用类类型编写泛型超类的子类以及规则是什么?
-
变更通用名时应注意什么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
- 这是不可接受的。
例如,如果您有其他课程SomeOtherInformationImpl
实现该接口,该方法的调用者将被允许编写:
SomeOtherInformationImpl i = InformationImpl.of();
但你的方法不返回SomeOtherInformationImpl
.
第二of()
方法与第一种方法有同样的问题。
如果您使用以下方法实例化您的类:
InformationImpl i = new InformationImpl<SomeOtherInformationImpl>();
the of()
方法必须返回一个SomeOtherInformationImpl
, not a InformationImpl
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)