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

Kan*_*mar 6 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>
Run Code Online (Sandbox Code Playgroud)

这样我就可以更清楚地解决 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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Case_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
Run Code Online (Sandbox Code Playgroud)
  1. 我想知道如何同时解决 Case_2、PROB_1 和 PROB_2?

  2. 如何为具有类类型的泛型超类编写子类以及规则是什么?

  3. 在子类化时将泛型更改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>
    
    Run Code Online (Sandbox Code Playgroud)

Era*_*ran 1

在第一个of()方法中,该方法可以返回任何实现 的类型InformationIfc,但是您的方法总是返回一个特定的实现 -InformationImpl这是不可接受的。

例如,如果您有一些SomeOtherInformationImpl实现该接口的其他类,则该方法的调用者将被允许编写:

SomeOtherInformationImpl i = InformationImpl.of();
Run Code Online (Sandbox Code Playgroud)

但你的方法不返回SomeOtherInformationImpl.

第二种of()方法与第一种方法有同样的问题。如果您使用以下方法实例化您的类:

InformationImpl i = new InformationImpl<SomeOtherInformationImpl>();
Run Code Online (Sandbox Code Playgroud)

of()方法必须返回 a SomeOtherInformationImpl,而不是 a InformationImpl