Usage learning of constructor

xiaoxiao2021-03-06  41

1. If you do not write a constructor for class, the compiler creates a default constructor and calls it when you are creating a new object.

2. Content function between constructor

Using system;

Namespace Webtest

{

///

/// Manager's summary description.

///

Public Class Manager

{

Public Manager (): this ("Test", 0)

{}

Public Manager (String Name): this (name, 0)

{}

Public Manager (String Name, Int grade)

{

THIS.GRADE = GRADE;

THIS.NAME = Name;

}

PRIVATE STRING MNAME;

Public String Name

{

get

{

Return this.mname;

}

set

{

THIS.MNAME = VALUE;

}

}

Private Int mgrade;

Public Int grade

{

get

{

Return this.mgrade;

}

set

{

THIS.MGRADE = VALUE;

}

}

}

3. Call the constructor of the base class

Each derived class implicitly or displays the constructor of the base class, in fact, if the constructor of the quasi-class class is not displayed, the CLR will call the unparalleled constructor.

The constructor of the base class can only be called from the constructor of the derived class. Call the implementation of the derived class for the implementation of the derived class.

4. Private constructor

Private constructor is typically included in many static methods, these static methods are used as a library, not an object. Adding a constructor will ensure that the class cannot be created externally. This is also used in many design patterns.

2 points of using a private constructor are:

1) The private constructor cannot be created outside.

2) If the parameters are not provided, the object created is meaningless.

a) No matter whether the specified or not, the derived class is to call the constructor of the base class. This means that classes with private constructors cannot be used as base classes, as their constructor is invisible to the school.

b) When another class nested in a class with a private constructor, a class containing a private constructor can be created by other objects. Regardless of the object is private, nesting constructor can always create instances of its container classes.

Using system;

Namespace Webtest

{

///

/// TEACHER's summary description.

///

Public Class Teacher

{

Private teacher ()

{

//

// TODO: Add constructor logic here

//

}

Public Teacher (String Name)

{

THIS.NAME = Name;

}

PRIVATE STRING MNAME;

Public String Name

{

get

{

Return this.mname;

}

set

{

THIS.MNAME = VALUE;

}

}

Private Int mgrade;

Public Int grade

{

get

{

Return this.mgrade;

}

set

{

THIS.MGRADE = VALUE;

}

}

}

}

5. Static constructor

The static constructor is not overloaded, so the available static constructor has only one default parameter constructor. The static constructor cannot be displayed and cannot be inherited in the derived class, but can be called when creating a base class type. Several principles:

1) Static constructor calls before the instance of creating classes

2) The static constructor is called before the first instance of the creation class.

3) The static constructor is called before reference to the static field.

转载请注明原文地址:https://www.9cbs.com/read-57885.html

New Post(0)