class Program
{
static void Main(string[] args)
{
HomeFurniture homeFur = new HomeFurniture();
Check<HomeFurniture,Furniture>(homeFur);
OfficeFurniture officeFur = new OfficeFurniture();
Check<OfficeFurniture,Furniture>(officeFur);
}
public static void Check<U,T>(T check) where T:Furniture
where U:T
{
// I would like to set the properties of HomeFurniture as
// MyProperty = 9;
// and that of OfficeFurniture as
// MyProperty = 10;
}
public class HomeFurniture : Furniture
{
public int MyProperty { get; set; }
}
public class OfficeFurniture: Furniture
{
public int MyProperty { get; set; }
}
public class Furniture
{
}
}
最佳答案
为什么不在Furniture
类中声明MyProperty
然后覆盖它?
例子:
public class Furniture
{
public virtual int MyProperty {get;}
}
public class HomeFurniture : Furniture
{
public override int MyProperty
{
get
{
return 9;
}
}
}
public class OfficeFurniture : Furniture
{
public override int MyProperty
{
get
{
return 10;
}
}
}
关于c# - C# 中的泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/660967/