Basic knowledge of C #: Basics (5) Overloading of methods

Source: Internet
Author: User
Teachers all have lectures this method, a teacher first in the western remote mountainous area, is standing in the classroom wood blackboard before lectures, after a few years performance good, transferred to a slightly better city, is sitting in front of the classroom with multimedia equipment lectures, and after a few years of doctor, after graduation, continue to be a teacher, But it is now lying at home and teaching the computer remotely. It's all lectures. This method has different execution process and output results under different conditions (different parameters). This is the overload.
Overloading is defined as: in the same class, or a subclass of this class, there are several methods with the same name that are overloaded, but the method has the same name but the argument list must be different. In the case of subclasses, a subclass has a method with the same name as the parent class, but differs from the argument list, and the method of the parent class must be of type protected and public.
Look at the following code:
After the school entrance examination, there are several admitted to Peking University and Tsinghua University, so the school asked the teacher to five-star hotel to eat. Welcome to the door to meet customers, to address: men/women, welcomed!

Using System;namespace Yys.        csharpstudy.mainconsole{public class Yschool {private int id = 0; private string name = String.        Empty;            public int ID {get {return this.id;            }} public string name {get {return Name;            }} public Yschool () {this.id = 0;        THIS.name = @ "Middle School of Tsinghua University";            } public yschool (int ID, string name) {this.id = ID;        THIS.name = name;            }///<summary>//constructor///</summary> public yschool (int id) {            This.id = ID;        THIS.name = @ "The middle School of Shaanxi Normal University";        }} public class Yteacher {private int id = 0; private string name = String.        Empty;        Private Yschool school = null; private string introDuction = String.        Empty; private string ImagePath = StriNg.        Empty;            public int ID {get {return id;            }} public string name {get {return Name;                }} public Yschool School {get {if (School = = null)                {School = new Yschool ();            } return School;            } set {school = value;            }} public string IntroDuction {get {return IntroDuction;            } set {introDuction = value;            }} public string ImagePath {get {return ImagePath;            } set {ImagePath = value; }}///<summary>//constructor///</summary> public yteacher (int ID, string name) {this.id = ID;        THIS.name = name; }///<summary>//constructor///</summary> public yteacher (int ID, string name, Yschoo            L School) {this.id = ID;            THIS.name = name;        This.school = School;             }///<summary>///For Students////</summary> public void toteachstudents () { Console.WriteLine (String.        Format (@ "{0} Teacher Education students: Good good Study,day day up!", this.name)); }///<summary>//Penalties for mistakes students///</summary>//<param name= "Punishmentconten T "></param> public void Punishmentstudents (string punishmentcontent) {Console.WriteLine (String. Format (@ ' {0} ' {1} teacher let the student who made the mistake {2}. ", this.        School.name, THIS.name, punishmentcontent)); }} public class Mrteacher:yteacher {public MrteachER (int id, string name): Base (ID, name) {}///<summary>///Extended method, Shave method. </summary> public void Shave () {Console.WriteLine (string. Format (@ "{0}" the teacher shaved with a flying razor. ", this.        Name)); }} public class Misteacher:yteacher {public misteacher (int ID, string name): Base (ID, name ) {}///<summary>///extension method, skincare method///</summary> public void Skinca Re () {Console.WriteLine (string. Format (@ "{0}" the teacher uses Chanel skin cream for skincare. ", this.        Name)); }} public class Fivestarshotel {//<summary>///reload//</summary> Pub LIC void Welcome (Mrteacher mteacher) {Console.WriteLine (@ "), welcome!        ");        }//<summary>//heavy-duty//</summary> public void Welcome (Misteacher misteacher) {Console.WriteLine (@ "Ladies, welcome!) ");        }    }} 
Using System;namespace Yys. csharpstudy.mainconsole{    class program    {        static void Main (string[] args)        {            Fivestarshotel Hotel = new Fivestarshotel ();            Mrteacher mrteacher = new Mrteacher (1, @ "Bull Rumble");            Console.WriteLine (@ "The Bull is Roaring");            Hotel. Welcome (Mrteacher);//male teacher door            misteacher misteacher = new Misteacher (2, @ "郝漂 Liang");            Console.WriteLine (@ "郝漂 Liang");                        Hotel. Welcome (Misteacher);//female teacher in the door            Console.readkey ();}}    

Results:

Looking at the code above, the constructor in Yteacher,yschool is the use of overloading.
The advantage of overloading is that logic can be more explicit, such as the above code, the Welcome method can actually write a method, and then use if else or switch statement to determine the final output results. But we do a project not only to complete a function, but also to make the code readable, logical and easy to maintain, so let the code logically closer to the real-world logic. Using overloading can make your code better understood, and the steps to perform are straightforward.

The above is the basic knowledge of C #: Basic Knowledge (5) method of overloaded content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.