Knowledge omission of inherited Constructors

Source: Internet
Author: User
Tags class manager

 

The default constructor of a class is a non-parametric constructor. When a subclass inherits the parent class, it must first call the parent class constructor. Therefore, when the parent class function defines a constructor with parameters, the subclass must call base to call the constructor of the parent class.

 

Constructor is a class method that is executed when an object of the given type is created. A constructor has the same name as a class. It usually initializes data members of a new object.

In the following example, a simple constructor namedTaxi. Then use
The new operator is used to instantiate the class. After allocating memory for the new object,NewOperator call now
TaxiConstructor.

C #
public class Taxi{    public bool isInitialized;    public Taxi()    {        isInitialized = true;    }}class TestTaxi{    static void Main()    {        Taxi t = new Taxi();        System.Console.WriteLine(t.isInitialized);    }}

A constructor without parameters is called a "default constructor ". Whenever you useNewThe operator instantiates an object and is notNewIf any parameter is provided, the default constructor is called. For more information,

 

Unless the class is
Otherwise, the C # compiler will provide a common default constructor for classes without constructor so that the class can be instantiated. For more information, see static and static class members.

You can set the constructor as a private constructor to prevent classes from being instantiated, as shown below:

C # class nlog
{
// Private constructor:
Private nlog (){}

Public static Double E = system. Math. E; // 2. 71828...
}

 

The constructor of the structure type is similar to the constructor of the class,
StructsExplicit default constructor cannot be included because the compiler automatically provides a constructor. This constructor initializes each field in the structure to the default value displayed in the default value table. However, only when the structure is used
NewThis default constructor is called only when it is instantiated. For example, the following code uses
Int32 default constructor, so you can be sure that the integer has been initialized:

 
int i = new int();Console.WriteLine(i);

However, the following code causes the compiler error cs0165 because it is not used
NewAnd tries to use an object that has not been initialized:

Copy code

int i;Console.WriteLine(i);

Based onStructsObjects can be initialized or assigned values, as shown below:

Copy code

int a = 44;  // Initialize the value type...int b;b = 33;      // Or assign it before using it.Console.WriteLine("{0}, {1}", a, b);

Therefore, it is not necessary to call the default constructor for the value type.

Class andStructsYou can define constructors with parameters. The constructor with parameters must passNewStatement or

Base statement. Class andStructsYou can also define multiple constructors without defining the Default constructors. For example:

C #

Copy code

public class Employee{    public int salary;    public Employee(int annualSalary)    {        salary = annualSalary;    }    public Employee(int weeklySalary, int numberOfWeeks)    {        salary = weeklySalary * numberOfWeeks;    }}

This class can be created using either of the following statements:

C #

Copy code

Employee e1 = new Employee(30000);Employee e2 = new Employee(500, 52);

Constructors can useBaseKeyword to call the constructor of the base class. For example:

C #

Copy code

public class Manager : Employee{    public Manager(int annualSalary)        : base(annualSalary)    {        //Add further instructions here.    }}

In this example, the base class constructor is called before the constructor block is executed.BaseThe keyword can be used with or without parameters. Any parameters of the constructor can be used
BaseOr as part of the expression. For more information, see
Base.

In a derived class, if you do not useBaseIf a base class constructor is explicitly called by a keyword, the default constructor is called implicitly (if any ). This means that the following constructor Declaration has the same effect:

C #

Copy code

public Manager(int initialdata){    //Add further instructions here.}

C #

Copy code

public Manager(int initialdata) : base(){    //Add further instructions here.}

If the base class does not provide the default constructor, the derived class must useBaseExplicitly call the base constructor.

Constructors can use
This keyword calls another constructor in the same object. AndBaseSame,ThisCan be used with or without parameters. Any parameters in the constructor can be used
ThisOr as part of the expression. For example, you can useThisOverride the second constructor in the previous example:

C #

Copy code

public Employee(int weeklySalary, int numberOfWeeks)    : this(weeklySalary * numberOfWeeks){}

AboveThisThis constructor is called because of the Keyword:

C #

Copy code

public Employee(int annualSalary){    salary = annualSalary;}

Constructor can be marked
Public, private, protected, internal
OrProtectedinternal. These access modifiers define how users of a class construct the class. For more information, see access modifiers.

Use
The static keyword can be used to declare a constructor as a static constructor. Static constructors are automatically called before accessing any static fields. They are usually used to initialize static class members. For more information, see static constructors.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.