public class Parent
{
Child child1;
public Parent()
{
child1.Name = "abc";
}
...
}
获取 NullReferenceException。我认为 Parent() 构造函数首先调用 Child() 构造函数,以便稍后可以在 Parent() 构造函数中访问 child1 对象???
最佳答案
您需要创建子级的实例;要么按照您定义的方式初始化它:
Child child1 = new Child();
或者在父构造函数中:
public Parent(){
child1 = new Child();
child1.Name = "Andrew";
}
关于java - 在.NET中,父类的构造函数首先调用其子类的构造函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3898699/