gpt4 book ai didi

子类化时Java泛型不兼容的类型

转载 作者:行者123 更新时间:2023-12-01 11:13:44 27 4
gpt4 key购买 nike

而从泛型类类型/形式类型参数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
  • 我想知道如何同时解决 Case_2、PROB_1 和 PROB_2?
  • 如何为具有类类型的通用父类(super class)编写子类以及规则是什么?
  • 更改泛型时应注意的事项 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/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com