为什么不能将子类型值分配给通用类型?

huangapple go评论74阅读模式
英文:

Why cannot assign a subtype value to a generic type?

问题

可以有人解释一下:

class Test<T extends BaseDao>{

    void someMethod(){
        T inst = new Dao();      // 需要类型 T,但提供了 Dao
        T inst2 = new BaseDao(); // 需要类型 T,但提供了 BaseDao
    }

}

class Dao extends BaseDao{}

我们将 T 类型定义为 BaseDao 的子类型。为什么不能将 Dao 或 BaseDao 类型赋值给 T 类型呢?

英文:

Could anybody explain:

class Test<T extends BaseDao>{

    void someMethod(){
        T inst = new Dao();      // required type T provided Dao
        T inst2 = new BaseDao(); // required type T provided BaseDao
    }

}

class Dao extends BaseDao{}

We have T type as a subtype of BaseDao.
Why cannot assign Dao or BaseDao types to T type?

答案1

得分: 0

T可以是Dao的一个实例,但是你不能像这样将Dao的新实例分配给泛型类型T,因为T可能是指向任何扩展BaseDao的类。

如果你需要有Dao的某个特定实例,你应该这样做:

Dao inst = new Dao()

如果相反地,你确实想要一个特定的实例,但又不想知道具体的类,你可以将inst1和inst2作为参数传递,或者传递一个字符串并根据名称创建一个实例,还有许多其他可能性。

简而言之,T inst = new Dao();这样做是没有意义的,因为它与泛型的目的冲突。

英文:

T can be an instance of Dao, but you cannot assign a new istance of Dao to the generic type T like this because T could refer to any class extending BaseDao.

If you need to have some specific instance of Dao you should do

Dao inst = new Dao()

If instead you do want a specific instance, but you don't want to know the specific class, you could pass inst1 and inst2 as parameters, or pass a string and create an instance by name, or many other possibility.

In short, T inst = new Dao(); makes no sense because it collides with the purpose of generics.

答案2

得分: 0

Imagine

    Test<MySpecialDao> specialDao = new Test<>();

Now, for a call like `specialDao.someMethod()`,    

    void someMethod(){
        T inst = new Dao();      // required type T, provided Dao
        T inst2 = new BaseDao(); // required type T, provided BaseDao
    }

effectively becomes    

    void someMethod(){
        MySpecialDao inst = new Dao();      // required type MySpecialDao, provided Dao
        MySpecialDao inst2 = new BaseDao(); // required type MySpecialDao, provided BaseDao
    }

and of course, both `Dao` and `BaseDao` aren't subtypes of `MySpecialDao`, probably supertypes.
    
I'd change to    

    void someMethod(){
        Dao inst = new Dao(); 
        BaseDao inst2 = new BaseDao();
    }

hoping that this change doesn't break your intended logic.
英文:

Imagine

Test&lt;MySpecialDao&gt; specialDao = new Test&lt;&gt;();

Now, for a call like specialDao.someMethod(),

void someMethod(){
    T inst = new Dao();      // required type T, provided Dao
    T inst2 = new BaseDao(); // required type T, provided BaseDao
}

effectively becomes

void someMethod(){
    MySpecialDao inst = new Dao();      // required type MySpecialDao, provided Dao
    MySpecialDao inst2 = new BaseDao(); // required type MySpecialDao, provided BaseDao
}

and of course, both Dao and BaseDao aren't subtypes of MySpecialDao, probably supertypes.

I'd change to

void someMethod(){
    Dao inst = new Dao(); 
    BaseDao inst2 = new BaseDao();
}

hoping that this change doesn't break your intended logic.

huangapple
  • 本文由 发表于 2020年10月14日 23:36:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64356825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定