[英]Specifying a generic constraint that depends on another generic class
I have something along the lines of the following: 我有以下几点:
// 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!
The above doesn't work for Controller
, because where U : Unit
isn't right since Unit
requires a generic parameter (like Unit<InstantAttack>
). 以上对
Controller
不起作用,因为where U : Unit
不正确,因为Unit
需要一个通用参数(如Unit<InstantAttack>
)。 I tried: 我试过了:
abstract class Controller<U<A>> where U<A> : Unit<A> {}
which certainly didn't work. 这当然没有用。 What's the correct syntax to do this?
这样做的正确语法是什么?
Either like this: 要么像这样:
abstract class Controller<U, A> where U : Unit<A> {}
Or like this: 或者像这样:
interface IUnit { }
abstract class Unit<A> : IUnit where A : Attack { }
abstract class Controller<U> where U : IUnit { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.