C # basic knowledge sorting: hiding basic knowledge (7) Methods

Source: Internet
Author: User

As mentioned in inheritance and abstract classes, there are these relationships between child classes and parent classes:
Subclass directly uses the parent class method (but the parent class method must be of the public or protected type );
The subclass method overwrites the parent class method (override );
The method of the subclass reloads the parent class method (overload );
See the following situation:

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 = @ "Shaanxi Normal University";} publi C 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; th Is. 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 /// Add the virtual keyword, this method can be overwritten and overwritten. /// </Summary> /// <Param name = "punishmentcontent"> </param> Public Virtual 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 universityteacher: yteacher {public universityteacher (int id, string name, yschool school): Base (ID, name, School) {}/// <summary> /// hide the method of the same name as the parent class. After hiding, this class can only access the hidden method and cannot access the method of the parent class. /// </Summary> Public new void toteachstudents () {console. writeline (string. Format (@ "{0}) Instructor Education Students: study. Net carefully! ", This. name);} // <summary> // overwrite /// </Summary> Public override void punishmentstudents (string punishmentcontent) {base. punishmentstudents (punishmentcontent); // You can also ignore the parent class method. // Your own code }}
Using system; namespace yys. csharpstudy. mainconsole {class program {static void main (string [] ARGs) {universityteacher uteacher = new universityteacher (3, @ "Dong Bian Cheng", new yschool (3, @ ""); // The access method is uteacher. toteachstudents (); // you can access the overwrite uteacher method. punishmentstudents (@ "let you take care of the subject. "); Yteacher Teacher = new universityteacher (3, @" Dong Bian Cheng ", new yschool (3, @" Tsinghua University ")); // The Hidden method cannot be accessed. toteachstudents (); // you can access the overwrite method teacher. punishmentstudents (@ "Run 10000 meters. "); Console. readkey ();}}}

Result:

Subclass inherits the method of the parent class and uses new to modify a method with the same name as the parent class. The new method with the same parameter list is called hidden. That is, the subclass hides this method of the parent class. However, hiding and overwriting are different. Hidden methods can only be accessed through the class where the method is located. If the parent class variable is used, hidden methods are still accessed.
From the code above, we can see the difference between overwriting and hiding. After a parent class variable references a subclass instance, it can only access hidden methods, but cannot access hidden methods. However, you can access the overwrite method.
Another point is that if you want to overwrite the quilt class of this method, you must add virtual to the parent class. You can also hide the New Keyword of the method of the parent class. Hiding is rarely used, and some problems are solved in some special cases.

Code download: http://download.csdn.net/detail/yysyangyangyangshan/4399465

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.