C # Constructor

Source: Internet
Author: User

// The constructor name must be the same as the class name and has no returned value.

Class A: object {// This indicates that Class A inherits the object

Public A (): Base () {}// this indicates that the constructor of the parent class object is referenced.

}

 

// The concept of this is not the class itself, but the instance after the class is created

 

// Call the no-argument Constructor

Class B {
Public B (){}

Public B (int I): this () {}//: this () indicates that the no-argument constructor of B is called first.

Public B (string S): this () {}//: this () indicates that the no-argument constructor of B is called first.

}

 

// If Class B inherits from Class A, Class B calls the no-argument constructor of Class A by default. If Class A has a constructor containing parameters, you must compile a non-argument constructor in Class A display.

 

// If Class B needs to call a parameter constructor of Class A, the following is required:

Class {

Public (){}

Public A (int I ){}

}

 

Class B: {
Public B (): Base (3) {}// call the parameter constructor of Class

}

 

Static constructor: a static constructor is a method member that initializes a class. It is generally used to initialize static data. Static constructor cannot have parameters, modifiers, and call. When a class is loaded, the static constructor of the class is automatically called.

Main role: Initialize static members of the class

Note:

1. During the execution of a program, the static constructor can only execute once at most.

2. The static constructor is executed after the static members of the class are initialized.

3. The static constructor is executed before any static member of the class is referenced.

4. The static constructor is executed before the instance variables of any class are assigned.

Class {
Static A () {}// static Constructor

}

 

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.