指定依赖于另一个泛型类的泛型约束
本文关键字:泛型 约束 泛型类 另一个 依赖于 | 更新日期: 2023-09-27 17:59:50
我有以下内容:
// This part is fine
abstract class Attack {}
abstract class Unit<A> where A : Attack {}
class InstantAttack : Attack {}
class Infantry : Unit<InstantAttack> {}
// I'm lost here, on this part's syntax:
abstract class Controller<U> where U : Unit {}
// I want the above class to take any kind of Unit, but Unit is a generic class!
以上内容不适用于Controller
,因为where U : Unit
是不对的,Unit
需要一个泛型参数(如Unit<InstantAttack>
)。我试过了:
abstract class Controller<U<A>> where U<A> : Unit<A> {}
这当然不起作用。正确的语法是什么?
要么这样:
abstract class Controller<U, A> where U : Unit<A> {}
或者像这样:
interface IUnit { }
abstract class Unit<A> : IUnit where A : Attack { }
abstract class Controller<U> where U : IUnit { }