Constructor, destructor, base, this

Source: Internet
Author: User
Constructor and destructor:

 
Using system; Class myclass {private int fnum; Public int num {get {return fnum ;}}/* the constructor has no return value, the default */Public myclass () {This. fnum = 2009;}/* can have multiple constructors with different parameters */Public myclass (int x) {This. fnum = x;} public myclass (Params int [] ARR) {foreach (int I in ARR) This. fnum + = I;}/* The Destructor has no parameters, no return values, no access modifier, and can only have one */~ Myclass () {// destructor is automatically called} class program {static void main () {myclass obj1, obj2, obj3; obj1 = new myclass (); console. writeline (obj1.num); // 2009 obj2 = new myclass (100); console. writeline (obj2.num); // 100 obj3 = new myclass (1, 2, 3); console. writeline (obj3.num); // 6 console. readkey ();}}

   

If there is no constructor or destructor, the default (or inheritance) will be used for new. A private constructor can prevent the class from being instantiated:

Using system; Class myclass {private myclass () {} public static void msg1 () {console. writeline ("msg1");} public static void msg2 () {console. writeline ("msg2") ;}} class program {static void main () {myclass. msg1 (); // msg1 myclass. msg2 (); // msg2 console. readkey ();}}

    

If a class has a non-default constructor, you cannot use the default constructor:

 
Using system; Class myclass {private int fnum; Public int num {get {return fnum ;}} public myclass (int x, int y) {This. fnum = x + y ;}} class program {static void main () {myclass OBJ; OBJ = new myclass (1, 2); console. writeline (obj. num); // 3 console. readkey ();}}

     

Static constructor:

The static constructor has no access modifier or parameter;
The static constructor is automatically called before new or any static member is called;
Static constructors are generally used to initialize static data;
A static constructor is triggered before the first new or first use of a static member;
Static constructor cannot be called directly.

Using system; Class myclass {public static int num; public static void shownum () {console. writeline (Num);} public void MSG () {console. writeline ("MSG");} static myclass () {num = 123 ;}} class program {static void main () {myclass. shownum (); // 123 myclass. num= 2009; myclass. shownum (); // 2009 myclass obj1 = new myclass (); obj1.msg (); // MSG console. readkey ();}}

      

Automatically call the constructor of the parent class:

Using system; Class Parent {public parent () {console. writeline ("parent") ;}} class Child1: parent {public Child1 () {console. writeline ("Child1");} public Child1 (int x) {console. writeline (x) ;}} class child2: Child1 {public child2 () {console. writeline ("child2");} public child2 (int x, int y) {console. writeline (x + y) ;}} class program {static void main () {parent P = new parent (); // parent Child1 C1 = new Child1 (); // parent/Child1 child2 C2 = new child2 (); // parent/Child1/child2 Child1 C11 = new Child1 (999 ); // parent/999 child2 c22 = new child2 (111,222); // parent/Child1/333 console. readkey ();}}

       

Base:

Using system; Class Parent {public parent () {console. writeline ("parent");} public parent (int x, int y) {console. writeline (x + y);} public parent (string S1, string S2) {console. writeline (S1 + S2) ;}} class Child1: parent {public Child1 () {console. writeline ("Child1") ;}} class child2: parent {public child2 (): Base () {console. writeline ("child2") ;}} class Child3: parent {public Child3 (): Base (111,222) {console. writeline ("Child3") ;}} class Child4: parent {public Child4 (): Base ("111", "222") {console. writeline ("Child4") ;}} class program {static void main () {Child1 C1 = new Child1 (); // parent/Child1 child2 C2 = new child2 (); // parent/child2 Child3 C3 = new Child3 (); // 333/Child3 Child4 C4 = new Child4 (); // 111222/Child4 console. readkey ();}}

        

This:

Using system; Class myclass {private string FS = "ABC-"; Public myclass () {console. writeline ("myclass");} public myclass (string Str) {console. writeline (this. FS + Str);} public myclass (INT num): this () {console. writeline (Num);} public myclass (int x, int y): This ("XYZ") {console. writeline (x + y) ;}} class program {static void main () {myclass C1 = new myclass (); // myclass C2 = new myclass ("EFG "); // ABC-EFG myclass C3 = new myclass (123); // myclass/123 myclass C4 = new myclass (111,222); // ABC-XYZ/333 console. readkey ();}}

         

Constructors, attributes, and base:

 
 using system; abstract class parent {private byte FID; Public parent (byte n) {FID = N ;} public byte ID {get {return FID;} set {FID = value ;}}} class child: parent {public child (byte myid): Base (myid) {}} class program {static void main () {Child rect = new child (6); console. writeline (rect. ID); // 6 rect. id = 8; console. writeline (rect. ID); // 8 console. readkey () ;}

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.