【发布时间】:2010-03-30 10:06:17
【问题描述】:
我是 OOP 的新手,想知道您能否就以下问题的最佳解决方案提供一些帮助。
public class GenericClass
{
private List<GenericListItem> _listItems;
}
public class SpecificClass : GenericClass
{
// I'd like to change the listItems specialisation to SpecificListItem (inherits from GenericListItem)
}
我将如何实现该系列的专业化变化?添加到集合并覆盖以指定正确的类型(SpecificListItem)时,我是否应该创建一个虚拟方法?然后在访问时将其转换为SpecificListItem?
请接受我的道歉,这是我的第一篇文章,所以可能含糊不清。如果我需要提供更多信息,请告诉我。另外,我很抱歉我对这个问题的无知!
非常感谢,
山姆
【问题讨论】:
-
如果您的 specificListItem 继承自 GenericListitem,那么您可以直接转换它,但您只会获得 GenericListItem 中的属性的信息
标签: c# oop polymorphism