Inheritance and Reference methods for C # constructors Override

Source: Internet
Author: User

As you all know, C # Constructors are primarily used to set the initial values of properties in a class, but it is often overlooked that the constructor method of a class can also refer to a constructor method in the calling parent class or other constructor method of itself, as in a method. Often so write a lot of duplicate code. The following code describes several uses of the class's construction method.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacepractise1{classProgram {Static voidMain (string[] args) {Testcon con=NewTestcon (); Console.WriteLine (Con.            ToString ()); TestA testA1=NewTestA (); Console.WriteLine ("Test Class A non-parametric construction method");            Console.WriteLine (Testa1.tostring ());            Console.WriteLine (); TestA testA2=NewTestA ("Set First Param"); Console.WriteLine ("test Class A A parameter construction method");            Console.WriteLine (Testa2.tostring ());            Console.WriteLine (); Testb testB1=NewTestb (); Console.WriteLine ("Test Class B No-parameter construction method");            Console.WriteLine (Testb1.tostring ());            Console.WriteLine (); Testb testB2=NewTESTB ("Set First Param"); Console.WriteLine ("Test Class B A parameter construction method");            Console.WriteLine (Testb2.tostring ());            Console.WriteLine (); Testb testB3=NewTESTB ("Set First Param","Set Second Param"); Console.WriteLine ("Test Class B Two parameter construction method");            Console.WriteLine (Testb3.tostring ());            Console.WriteLine (); Testb testB4=NewTESTB ("Set First Param","Set Second Param","Set Third Param"); Console.WriteLine ("Test Class B Three parameter construction method");            Console.WriteLine (Testb4.tostring ());            Console.WriteLine ();        Console.ReadLine (); }        classTestcon {Private string_a ="_A1"; Private string_b ="_b2"; }        /// <summary>         ///Test Class A/// </summary>         classTestA {protected string_testvaluea; /// <summary>             ///Non-parametric construction method/// </summary>              PublicTestA (): This("Set First Param")            {            }            /// <summary>             ///a method for constructing parameters/// </summary>             /// <param name= "value" ></param>              PublicTestA (stringvalue) {_testvaluea=value; }            /// <summary>             ///re-tostring Method/// </summary>             /// <returns></returns>              Public Override stringToString () {return  This. _testvaluea; }        }        /// <summary>         ///test class Testb, inheriting from the Testa class/// </summary>         classTestb:testa {protected string_testvalueb; protected string_testvaluec; /// <summary>             ///calling a construction method in a parent class/// </summary>              PublicTESTB ():Base()            {                 This. _testvalueb ="Set Second Param";  This. _TESTVALUEC ="Set Third Param"; }            /// <summary>             ///calling a construction method in a parent class/// </summary>             /// <param name= "Valuea" ></param>              PublicTESTB (stringValuea):Base(Valuea) { This. _testvalueb ="Set Second Param";  This. _TESTVALUEC ="Set Third Param"; }            /// <summary>             ///calling other constructor methods/// </summary>             /// <param name= "Valuea" ></param>             /// <param name= "Valueb" ></param>              PublicTESTB (stringValuea,stringValueb): This(Valuea, Valueb,"Set Third Param")            {            }            /// <summary>             ///three methods for constructing parameters/// </summary>             /// <param name= "Valuea" ></param>             /// <param name= "Valueb" ></param>             /// <param name= "Valuec" ></param>              PublicTESTB (stringValuea,stringValueb,stringVALUEC) {                 This. _testvaluea =Valuea;  This. _testvalueb =Valueb;  This. _TESTVALUEC =Valuec; }            /// <summary>             ///re-tostring Method/// </summary>             /// <returns></returns>              Public Override stringToString () {return  This. _testvaluea +"\ n"+ This. _testvalueb +"\ n"+ This. _testvaluec; }        }    }}

Inheritance and Reference methods for C # constructors Override

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.