随着结构继承类..实现加法与泛型
abstract class Unit
{
int Id;
}
class Measure : Unit
{
int Current;
int Baseline;
}
class Weight : Unit
{
int Minimum;
int Maximum;
int Current;
}
我基本上要增加一个“添加”方法添加,比如,两种方法一起使用,或添加两个砝码在一起。但它需要在Unit基类中。所以基本上,如果我有
List<Units> units = new List<Unit>();
List<Units> otherUnits = new List<Unit>();
// populate units with various Measures and Weights.
// populate otherUnits with various Measures and Weights.
foreach(Unit u in units)
{
u.Add(
// add something from the otherUnits collection. Typesafe, etc.
);
}
我已经试过..
public abstract T Add<T>(T unit) where T : Unit;
在股类
,但我得到它是如何不是在继承类的适当的标识符,当我尝试错误用适当的类填充“T”。有任何想法吗?
我的单元类必须参与EntityConfiguration。所以我不能让它成为一个通用类型。有没有其他方法?哦,我明白了 - 你增加了另一种方式。我认为这可能有效。让我尝试一下并找出答案。 –
Ciel
2010-09-03 19:59:31
@Stacey - 我的选择应该在'Unit'类中工作。所以你不需要把它抽象化。 – GenericTypeTea 2010-09-03 20:06:14