C # execution sequence of constructors in the inheritance Process

Source: Internet
Author: User
Tags types of functions

Using system;

 

Namespace Zoo

{

// ************ Define the base class animal

Public class animal

{

Static string baseclassname;

// Public String eatkind;

Protected string _ skincolor;

Static animal ()

{

Baseclassname = "animal ";

// Eatkind = "meat ";

Console. writeline (baseclassname );

// Console. writeline (eatkind );

Console. writeline ();

}

Public animal ()

{

Console. writeline ("an animal was born !!! ");

Console. writeline ();

}

Protected animal (string skincolor)

{

This. _ skincolor = skincolor;

Console. writeline ("the color of this animal is:" + this. _ skincolor );

Console. writeline ();

}

}

// ************ Define the subclass horse to inherit the parent class animal

Public class horse: Animal

{

Public horse () //: Base ()

{

_ Skincolor = "red rabbit and horse ";

Console. writeline ("A pony was born .... ");

Console. writeline ("the color of this pony is:" + this. _ skincolor );

Console. writeline ();

}

Double _ weight;

String _ sex;

Public horse (string skincolor, string sex, double weight): Base (skincolor)

{

_ Skincolor = skincolor;

_ Weight = weight;

_ Sex = sex;

// Base. _ skincolor = skincolor;

Console. writeline ("A pony was born .... ");

Console. writeline ("this pony is:" + this. _ sex );

Console. writeline ("the color of this pony is:" + this. _ skincolor );

Console. writeline ("the weight of this pony is:" + this. _ weight );

Console. writeline ();

}

}

// ************* Define the test demo

Public class demo

{

Public static void main (string [] ARGs)

{

// Animal A = new animal ();

// Horse H = new horse ();

Horse HH = new horse ("pure white", "male", 320 );

Horse hhh = new horse ("solid color", "female", 120 );

}

}

}

Test the results of the main () method in the demo class under different circumstances and study the execution process of the constructor.

1. Run only animal A = new animal ();

Result: Animals

An animal was born !!!

Execution Process: first, execute the static constructor static animal () of the base class animal, and execute the Public Non-argument constructor public animal () when instantiating ();

Note: The static constructor of a class belongs to the class and can be accessed without generating an instance. It is used to load an application containing the class.ProgramBut the static constructor Cannot initialize the instance variables of the class. The instance members of the class belong to the instance of the class and cannot be accessed without creating an instance object. The instance members can be static members of the class and other instance members. The static members of a class belong to this class, and non-static members belong to an instance of this class.CodeIs the class constructor. Constructors are special types of functions used to initialize objects.

2. Run only horse H = new horse ();

Result: Animals

An animal was born !!!

A pony was born .......

The color of the pony is red.

Execution Process: first, execute the static constructor static animal () of the base class animal, and then execute the Public Non-argument constructor public animal () when instantiating the base class animal (); then, call the non-argument constructor public horse () of the horse class ()

3. Run only horse H = new horse ("pure white", "male", 320 );

Result: Animals

The color of the animal is pure white.

 

A pony was born .......

This pony is male

The color of the pony is pure white.

The weight of this pony is 320.

Execution Process: first, execute the static constructor static animal () of the base class animal, and then execute the protected with parameter constructor public animal (string skincolor) when instantiating the base class animal ); then, call the Public horse (string skincolor, string skincolor, double weight): Base (skincolor) with parameters of the horse class)

4. Run only horse H = new horse ("pure white", "male", 320 );

Horse HH = new horse ("solid color", "female", 120 );

Result: Animals

The color of the animal is pure white.

 

A pony was born .......

This pony is male

The color of the pony is pure white.

The weight of this pony is 320.

 

The color of the animal is solid color.

A pony was born .......

This pony is: Female

The color of this pony is solid color.

The weight of this pony is 120.

Note: The static constructor of a class can be executed at most once in a given application domain: only the static constructor of the class is activated when an instance of the class is created or any static member of the referenced class is activated;

 

Finally, let's make a summary:
1. The general principle is that the inherited parent class is constructed first and then inherited from the Child class of the parent class;

2. Static constructor. Static Parameters take precedence over non-static constructor and non-static parameter constructor;

3. Static constructor:
(1) It is used to initialize static and read-only fields.
(2) You cannot add access modifiers when adding static keywords because static constructors are private.
(3) Static constructor of a class can be executed at most once in a given application domain: only the static constructor of the class is activated when an instance of the class is created or any static member of the referenced class is activated.
(4) Static constructors cannot be inherited and cannot be called directly.

4. constructor is called when a class object is created. It is used to initialize fields in the class and complete some calls.
5. constructor is called when constructing an object. If you are not enough to construct an object, how can you use the constructor. Of course, the parent class function cannot be inherited in the subclass. If it can be inherited, you can call the constructor In the subclass. When creating an object, the constructor is called by the system management. We cannot call the constructor of the class ourselves. When subclasses are implemented, they first call the parent class constructor to instantiate the parent class. Then the subclass is instantiated.

 

After writing it, the learning of the C # constructor is coming to an end. Next, the interface learning is coming!

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/ben_0788/archive/2008/06/07/2520230.aspx

 

Related Article

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.