【发布时间】:2021-11-17 23:19:40
【问题描述】:
在我的项目中,我有几个类继承自一个父类。我想定义一个列表,其中包含每个子类的对象。
public class Parent
{
public string name;
}
public class Child1 : Parent
{
public int property1;
}
public class Child2 : Parent
{
public int property2;
}
public List<Parent> childrenOfParent = new List<Parent>(){
new Child1(),
new Child2()
};
这是否可行,还是必须为各个子类定义一个列表?
【问题讨论】:
-
定义这行得通吗。 -- 你只需要
name属性吗?你在你的类中实现了任何接口吗? -
你为什么不试试?
-
如果您不在命名空间中,它会起作用! ??????
标签: c# .net list types collections