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

标签 java generics

而从泛型类类型/形式类型参数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/

    相关文章:

    swift - 无法检查类型 T : Equatable inside a generic 的值是否相等

    java - 当涉及到 finally block 的返回值时,在 try block 中使用 return 语句是否是一种好习惯

    c# - 使用动态类型调用泛型方法

    java - 无法通过单击关闭 X 按钮关闭框架

    java - 无法使用首选项编辑器在单独的类中创建 AlertDialog

    java - 扩展一个类,其中父类的参数扩展了一个类

    c# - 如何向上转换泛型类型参数

    java - 通过自动/拆箱操作泛型

    采用泛型类型参数的 Java 生成类

    java - Telnet session 与 TCP session