Usage One:
The constructor of the parent class is always executed before the child class. Initializes a static constructor, and then initializes the subclass constructor.
public class Basecircle {public basecircle () {Console.WriteLine ("No arguments base constructor !!!"); } Public basecircle (double arg) {Console.WriteLine ("Double arg base constructor!!!"); }} public class Subcircle:basecircle {public subcircle (): Base () {Console.WriteLine ("s UB Class no argument constructor,actually call base constructor!!! ");} Public subcircle (Double A): base (a) {Console.WriteLine ("Sub class with argument, actually call base Doub Le constructor!!! ");} Public subcircle (int. K): This (for) {Console.WriteLine ("Sub class with argument int K, actually call Sub class constructor int I & J!!! ");} Public Subcircle (Int. I,int j) {Console.WriteLine ("Sub class with int i&j argument!!!!"); }} static void Main (string[] args) {subcircle S1 = newSubcircle (); Subcircle s2 = new Subcircle (1.1); Subcircle s3 = new Subcircle (1);}
Output result: no arguments base constructor!!! Sub Class no argument constructor,actually call base constructor!!! Double ARG base constructor!!! Sub class with argument, actually call base double constructor!!! No arguments base constructor!!! Sub class with int i&j argument!!!! Sub class with argument int K, actually call sub class constructor int i & J!!!
Usage Two:
Is it a blur of these two keywords?
Ha, now I write code, the code is the most persuasive!
Class BaseClass {private int numA; Public BaseClass () {Console.WriteLine ("parameterless constructor of the base class. Value:{0} ", NumA); } public BaseClass (int i) {This.numa = i; Console.WriteLine ("The base class has a constructor with a parameter.") Value:{0} ", NumA); }} class Childclassa:baseclass {private int numB; Public Childclassa () {Console.WriteLine ("Sub-class parameterless constructor. Value:{0} ", NumB); } public Childclassa (int i) {this.numb = i; Console.WriteLine ("The subclass has a constructor with a parameter.") Value:{0} ", NumB); } public Childclassa (int i, int j): Base (i) {this.numb = J; Console.WriteLine ("Subclass" constructor with two parameters.) Value:{0} ", NumB); }} class Childclassb:baseclass {private int numB; Public Childclassb () {Console.WriteLine ("Sub-class parameterless constructor. Value:{0} ", NumB); } public childclassb (int i) { This.numb = i; Console.WriteLine ("The subclass has a constructor with a parameter.") Value:{0} ", NumB); } public Childclassb (int i, Int. j): This (i) {this.numb = J; Console.WriteLine ("Subclass" constructor with two parameters.) Value:{0} ", NumB); }} class Demo {static void Main (string[] args) {Console.WriteLine ("Use base\n"); Childclassa a = new Childclassa (2, 4); Console.WriteLine (); Console.WriteLine ("----------------------------------------\ n"); Console.WriteLine ("Use this\n"); CHILDCLASSB B = New CHILDCLASSB (2, 4); Console.readkey (); } }
The results of the implementation are as follows:
--------------------------------result----------------------------------Use the base base class constructor with one parameter. The Value:2 subclass has a constructor with two parameters. Value:4----------------------------------------Use the parameterless constructor of this base class. The value:0 subclass has a constructor with one parameter. The Value:2 subclass has a constructor with two parameters. Value:4--------------------------------Results--------------------------------
This is just the call itself, but it is necessary to call a constructor that does not have a reference to the base class, so a "parameterless constructor for the base class" is displayed. Value:0 ".
Base is a parameterized constructor that calls the base class.
Good text to the top concern me to collect the article
The difference between base and this in C # constructors