2013-02-17 75 views
0

我沿着以下线的东西:指定依赖于其他泛型类通用约束

// 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> {} 

这肯定没有工作。什么是正确的语法来做到这一点?

回答

2

无论是这样的:

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 { } 
+0

/捂脸。谢谢! – Cornstalks 2013-02-19 03:33:00