简体   繁体   English

在.NET中,父类的构造函数是否将其子类的构造函数称为第一件事?

[英]In .NET, does a parent class' constructor call its child class' constructor first thing?

public class Parent
{
    Child child1;

    public Parent()
    {
        child1.Name = "abc";
    }
    ...
}

Gets a NullReferenceException. 获取一个NullReferenceException。 I thought the Parent() constructor calls the Child() constructor first, so that it is possible to access child1 object later in the Parent() constructor??? 我以为Parent()构造函数首先调用Child()构造函数,以便以后可以在Parent()构造函数中访问child1对象?

You need to create an instance of the child; 您需要创建子实例; either initialize it as you define it: 可以按照定义对其进行初始化:

Child child1 = new Child();

Or in the Parent constructor: 或在Parent构造函数中:

public Parent(){
    child1 = new Child();
    child1.Name = "Andrew";
}

The parent class's constructor doesn't call constructors for its members. 父类的构造函数不会为其成员调用构造函数。 When the member is a reference, it's just set to null. 如果成员是引用,则将其设置为null。 You need to explicitly allocate it by calling child1 = new Child 您需要通过调用child1 = new Child来显式分配它

Members are not implicitly constructed. 成员不是隐式构造的。 They're initialized with their default values (ie null for reference type members), which is why your child1 member is null. 它们使用其默认值(即,引用类型成员为null )初始化,这就是为什么child1成员为null的原因。

You need to create an instance of child1 : 您需要创建child1的实例:

public Parent
{
  child1 = new Child();
}

On a sidenote, I think you are being confused by constructor call rules for inherited classes. 在旁注中,我认为您对继承的类的构造函数调用规则感到困惑。 If your Child class inherited your Parent class, the Parent class' default (ie parameterless) constructor would be implicitly called (if it existed): 如果您的Child类继承了您的Parent类,则将隐式调用Parent类的默认(即无参数)构造函数(如果存在):

class Parent
{
  protected string member;
  public Parent()
  {
    member = "foo";
  }
}

class Child : Parent
{
  public Child()
  {
    // member is foo here; Parent() is implicitly called
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么对父 class 构造函数的调用不调用在子 class 中覆盖的父方法? - Why does the call to a parent class constructor not call the parent's methods that are overriden in the child class? PHP-如果我们在php中创建子类的对象,它会调用父类构造函数,然后调用子类构造函数(如java)吗? - PHP -If we create object of child class in php, does it call parent class constructor and then child class constructor like java? 在Java中,您可以通过父类的子类的构造函数调用父类的超类构造函数吗? - In Java, can you call a parent class's superclass constructor through the constructor of the child class of the parent class? 有没有一种方法可以调用一个父类构造函数,该构造函数从一个不具有自己构造函数的子类中获取参数? - Is there a way I can call a parent class constructor that takes parameters from a child class that does not have a constructor of it's own? 即使子类已经有一个已定义的构造函数,父类是否总是需要一个默认或无参数的构造函数? - Does a parent class always need a default or no-argument constructor even if the child class already has a defined constructor? Java:子类构造函数类如何中和父构造函数? - Java: How Can be child class constructor class neutralize parent constructor? Java单例类不会在Android上调用其构造函数 - a Java singleton class does not call its constructor on Android Java:如何在子类构造函数中调用超类构造函数? - Java: How does a call to the super class constructor inside of a child class constructor work? 如何从子类构造函数调用超类的私有构造函数? - How to call private constructor of super class from child class constructor? 在Java中的子类中使用父构造函数 - Using parent constructor in a child class in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM