简体   繁体   中英

What is the difference between simple constructor and constructor with base?

I have some doubts in my below codes,

public class ClassA: BaseClass
{
    public ClassA(UIElement element)
        : base(element)
    {
    }
}

public class ClassA: BaseClass
{
    public ClassA(UIElement element)
    {
    }
}

What is the difference between the above codes?

Doubts:

1.It is necessary to call base constructor

2.What is the use to Call the base constructor

3.If we didn't call base constructor it calls implicitly.

Any one please provide suggestion for this.

the first one instructs to use the BaseClass(UIElement) constructor, while the other one will use BaseClass() to be initialised instead.

To your questions; it all depends on if it makes sense to initiase BaseClass with the passed element or not.

  1. Not always, if you don't specify anything then base() will be called
  2. base() as you already did
  3. Yes

In each case a base constructor is called, just implicitly and using the default in the latter case - in the first case it's using an explicit base constructor which accepts an argument. If no default parameterless constructor existed on the base class then an error would be encountered if it wasn't explicitly called by the user in order to provide the required input.

So, it's only necessary to call if you need to pass input to a specific constructor or a default public constructor is not defined.

1-3. no it's not necessary it call it automagically if the base calss contain a constructor without parameter 2. you use base when you inherit from another class, the part you inherited need to be constructed too so you have to call the base constructor

Please note 2 more things using chained contructors

  1. The Base Contructor is always called before the actual constructor.
  2. The constructors can have different accessabilities like private or public. You cannot chain every constructor from the base to the derived class.

The first one passes a parameter to the base constructor, while the second one uses the default (parameterless constructor).

public class Human
{
    public string Name { get; set; }
    public Human()
    {
        Name = "No name";
    }
    public Human(string name)
    {
        Name = name + " Jr.";
    }
}
public class Male : Human
{
    public Male() {}
    public Male(string name) : base(name) {}
}
public class Female : Human
{
    public Female() { Name = "Girl"; }
}


var unnamedHuman = new Human();
//unnamedHuman.Name == "No name"
var namedHuman = new Human("Jon");
//namedHuman.Name == "Jon"

var unnamedMale = new Male();
// unnamedMale.Name == "No name"
var namedMale = new Male("Chris");
// namedMale.Name == "Chris Jr";

var unnamedFemale = new Female()
// unnamedFemale.Name == "Girl"

The logic: When you initialize a child class (by calling their ctor), first its base class constructors are called in order (top-down), the final one would be your child class' constructor you invoked. By default, the default parameterless contructor is used but you can specify and pass arguments to the base constructor by using base(parameter1, p2) .

If your BaseClass looked like:

public class BaseClass
{
    private readonly _element;
    public BaseClass(UIElement element)
    {
       _element = element;
    }
}

Then you would absolutely have to call the base constructor, because it's the only constructor that BaseClass has.

If it looked like this:

public class BaseClass
{
    private readonly _element;
    public BaseClass()
    {
        _element = default(UIElement);
    }
    public BaseClass(UIElement element)
    {
       _element = element;
    }
}

Then you would absolutely have to call the base constructor if you wanted to set the element, as it can only be set in the constuctor.

If it looked like this:

public class BaseClass
{
    private _element;
    public BaseClass()
    {
        _element = default(UIElement);
    }
    public BaseClass(UIElement element)
    {
       _element = element;
    }
    public UIElement Element
    {
      get{ return _element; }
      set{ _element = value; }
    }
}

Then you could choose between:

public class ClassA: BaseClass
{
    public ClassA(UIElement element)
        : base(element)
    {
    }
}

public class ClassA: BaseClass
{
    public ClassA(UIElement element)
    {
       Element = element;
    }
}

However, the former is clearer and doesn't have a period where Element returns the default ( null ) during the overall construction.

In all, consider that ClassA is a type of BaseClass . In creating a sensible instance of ClassA you have to do everything entailed in setting up a sensible instance of BaseClass . It is the job of the constructor to make sure this happens. Lean toward calling the base constructor as much as is meaningful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM