2013-02-17 82 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