C# - 为什么我得到一个空的收藏? [英] C# - why am I getting an empty collection?
问题描述
为什么人收藏中有项目只有当我在子类中重写呢?下面的代码。如果我取消了重载方法,那么我的收藏有2人在公园里
公共类公园:事情$ B $ B {
}
公共抽象类的事情
{
公共虚拟目录<&东西GT;人们{搞定; } =新的List<&东西GT;();
}
公共类的人物:事情
{
事情磷;
公共字符串名称{;组; }
公众形象(事P)
{
this.p = P;
NAME =结婚;
this.p.people.Add(本);
}
//公众覆盖列表<&东西GT;人= GT; p.people;
}
公共类PersonB:事情
{
事情磷;
公共字符串名称{;组; }
公共PersonB(事P)
{
this.p = P;
NAME =乔;
this.p.people.Add(本);
}
//公众覆盖列表<&东西GT;人= GT; p.people;
}
和这里的测试应用程序:
公园东西=新园();
=园区新PERSONA(公园);
=园区新PersonB(公园);
Console.WriteLine(park.people.Count);
的事情公园=新园();
下面你实例化一个公园
对象,其分配给其类型的变量是的事情
。到目前为止,这么好
公园=新的人物(公园);
下面你实例化一个人物
,和因为你通过公园
对象的构造,该构造函数将自身添加到公园
的个人
集合。所以,该集合现在包含一个人。再次,到目前为止,一切都很好。
不过,你那么新的人物
对象分配给公园
变量。这不是一个运行时错误,因为变量的类型的事情
和人物
是事
,但它几乎肯定你一个逻辑错误,因为我无法想象的原因,你会想要一个叫做变量公园
这是指一个人。
关键一点是,在这一点上,<code> park.People 并非指对人的公园
对象的集合;它指的是人的人物
对象的集合,这是空的。
公园=新PersonB(公园);
现在,当你调用 PersonB
构造,你不能传递一个公园
对象;你传递它,你分配到公园
变量人物
对象。因此, PersonB
构造函数添加自己到人物
的人
集合,它现在包含一个人。
但同样,你分配结果公园
。所以现在公园
包含 PersonB
对象,其人
收集空。这就是为什么这样的:
Console.WriteLine(park.people.Count);
打印为零。
How come people collection has items in it only if I override it within the subclasses? Here's the code. If I uncomment the overridden methods, then my collection has 2 people in the park.
public class Park : Thing
{
}
public abstract class Thing
{
public virtual List<Thing> people { get; } = new List<Thing>();
}
public class PersonA : Thing
{
Thing p;
public string Name { get; set; }
public PersonA(Thing p)
{
this.p = p;
Name = "Marry";
this.p.people.Add(this);
}
//public override List<Thing> people => p.people;
}
public class PersonB : Thing
{
Thing p;
public string Name { get; set; }
public PersonB(Thing p)
{
this.p = p;
Name = "Joe";
this.p.people.Add(this);
}
//public override List<Thing> people => p.people;
}
And here's the test application:
Thing park = new Park();
park = new PersonA(park);
park = new PersonB(park);
Console.WriteLine(park.people.Count);
Thing park = new Park();
Here you are instantiating a Park
object and assigning it to a variable whose type is Thing
. So far, so good.
park = new PersonA(park);
Here you are instantiating a PersonA
, and because you pass your Park
object to the constructor, the constructor adds itself to the Park
's People
collection. So that collection now contains one person. Again, so far, so good.
However, you then assign the new PersonA
object to the park
variable. This is not a runtime error because the variable has type Thing
and a PersonA
is a Thing
, but it's almost certainly a logic error on your part, because I can't conceive of a reason why you'd want a variable called park
that refers to a person.
The critical thing is that at this point, park.People
doesn't refer to the Park
object's collection of people; it refers to the PersonA
object's collection of people, which is empty.
park = new PersonB(park);
Now when you invoke the PersonB
constructor, you're not passing it a Park
object; you're passing it the PersonA
object that you assigned to the park
variable. So the PersonB
constructor adds itself to the PersonA
's People
collection, which now contains one person.
But again, you're assigning the result to park
. So now park
contains a PersonB
object whose People
collection is empty. Which is why this:
Console.WriteLine(park.people.Count);
Prints zero.
这篇关于C# - 为什么我得到一个空的收藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!