How to inherit from a generic parameter?

后端 未结 4 2002
广开言路
广开言路 2020-12-11 14:23

I\'m basically wanting to do this:

class UILockable : T
    where T : UIWidget
{
}

However, this doesn\'t work. I\'ve seen people

4条回答
  •  忘掉有多难
    2020-12-11 15:17

    Think about it this way: When Type B inherits from type A, you're declaring that B is similar to A (where similarity means that you can use B everywhere you expect to use A). Now because such similarity is not symmetric you have that B is similar to A but A is not similar to B. Furthermore, B and C can both be similar to A (i.e. they both descend from A) without being similar to each other.

    So what you want to declare is that Unlockable is similar to everything that is similar to UIWidget, but that's impossible because type similarity is not transitive (i.e. if B is similar to A and C is similar to A you can't say B is similar to C).

提交回复
热议问题