【发布时间】:2018-08-28 09:53:58
【问题描述】:
我有一个继承自基类的类
还有两个类具有这些原始类的列表作为属性
我认为这两个新类应该具有继承性,但似乎无法正常工作。
public class Attribute
{
public string Name { get; set; }
public string ShortName { get; set; }
public bool Standard { get; set; }
}
public class BaseAttribute : Attribute
{
public int Value { get; set; }
}
public class Profile
{
public List<Attribute> AttributeList = new List<Attribute>
{
new Attribute {Name = "Hands", ShortName = "HA", Standard = true},
new Attribute {Name = "Arms", ShortName = "AR", Standard = true},
};
}
public class BaseProfile: Profile
{
public List<BaseAttribute> AttributeList
{ get; set; }
}
我可以更改继承的 AttributeList 的类型以将其扩展为包含每个元素的 value 属性吗? 或者我不应该在 BaseProfile 方面完全继承?
我已经尝试在此处(以及更广泛的互联网)进行搜索,并且有许多有助于简单继承的答案,但我找不到在继承期间更改属性类型的答案。
【问题讨论】:
-
首先开始支持composition over inheritance。其次,您可以在
Profile-class 中将AttributeList设为通用。 -
当
AttributeList是基类中的字段时,您不能将其更改为派生类中的属性,除非您使用 隐藏 基实现new-关键字。
标签: c# inheritance properties extends