C # basic knowledge sorting: Basic knowledge (5) method Overloading

Source: Internet
Author: User

The teacher has a way to give lectures. A teacher first lectures in a remote mountainous area in the west, standing in front of a wooden blackboard in the classroom. After several years of good performance, he has been transferred to a better city, I sat in front of the classroom and used multimedia equipment to give lectures. After a few years, I took my doctor's test and continued to work as a teacher after graduation. But now I am lying at home and remotely teaching to my computer. This method is used for lectures. Different parameters have different execution processes and output results. This is heavy load.
The definition of overload is: in the same class, or in the subclass of this class, there are several methods with the same name as overload, but the method has the same name but the parameter list must be different. In the subclass case, the subclass has the same method as the parent class method but different from the parameter list, and the method of the parent class must be of the protected and public type.
See the following code:
After the college entrance examination, several of them were admitted to Peking University and Tsinghua University, so the school invited the teacher to a five-star hotel for dinner. Welcome to the customer! Name: men/women. Welcome!

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 = @ "";} public yschool (int id, string name) {This. id = ID; this. name = Name;} // <summary> // constructor // </Summary> Public yschool (int ID) {This. id = ID; this. name = @ "中"";} 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 {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, Yschool) {This. id = ID; this. name = Name; this. school = school;} // <summary> // Method for giving lectures to students /// </Summary> Public void toteachstudents () {console. writeline (string. format (@ "{0} instructor Education Students: Good good study, day up! ", This. name ));} /// <summary> // Method for punishing students who make mistakes /// </Summary> /// <Param name = "punishmentcontent"> </param> Public void punishmentstudents (string punishmentcontent) {console. writeline (string. format (@ "{0}'s {1} teacher asked students who made mistakes {2 }. ", This. school. name, this. name, punishmentcontent) ;}} public class mrteacher: yteacher {public mrteacher (int id, string name): Base (ID, name) {}//< summary> /// Method for scaling and shaving. /// </Summary> Public void shave () {console. writeline (string. Format (@ "{0}) the instructor shave his beard with the Feike razor. ", This. name) ;}} public class misteacher: yteacher {public misteacher (int id, string name): Base (ID, name) {}/// <summary> /// extension method, skin care method /// </Summary> Public void skincare () {console. writeline (string. format (@ "{0} the instructor uses Chanel skin cream for skin care. ", This. name) ;}} public class fivestarshotel {/// <summary >/// overload /// </Summary> Public void welcome (mrteacher mteacher) {console. writeline (@ "Sir, welcome! ") ;}/// <Summary> /// reload /// </Summary> Public void welcome (misteacher) {console. writeline (@", welcome! ");}}}
Using system; namespace yys. csharpstudy. mainconsole {class program {static void main (string [] ARGs) {fivestarshotel Hotel = new fivestarshotel (); mrteacher = new mrteacher (1, @ ""); console. writeline (@ "awesome"); hotel. welcome (mrteacher); // enter the door for a male. misteacher = new misteacher (2, @ "haobaojing"); console. writeline (@ "hao qinglai"); hotel. welcome (misteacher); // enter the console as a female. readkey ();}}}

Result:

 

In the above Code, the constructor in yteacher and yschool is used for heavy load.
The advantage of overload is that the logic can be clearer. For example, in the above Code, the welcome method can also be used to write a method, and then use the if else or switch statement to judge and output the result. However, to complete a project is not only to complete a function, but also to make the code readable, clear logic, and easy to maintain. Therefore, the code should be logically closer to the real-world logic. Using overload can make the Code better understood and the execution steps are also intuitive.
Code download: http://download.csdn.net/detail/yysyangyangyangshan/4393502

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.