Basic knowledge of C #: The Basics (7) Hiding methods

Source: Internet
Author: User
As mentioned in the inheritance and abstract classes, there are these relationships between subclasses and methods of the parent class:
Subclasses directly use the parent class method (but the parent method must be public or protected type);
The subclass method overrides the parent class method (override);
Subclass method overloads the parent class method (overload);
Look at the following scenario:

    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 nam  E      {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} Teachers educate students: good good Study,day day up!", this.        Name));        }///<summary>///To punish the wrong student's method///Add virtual keyword to indicate that the method can be overwritten rewrite///</summary> <param name= "punishmentcontent" ></param> public virtual void punishmentstudents (String Punishmentco ntent) {Console.WriteLine (string. Format (@ ' {0} ' {1} teacher let the student who made the mistake {2}. ", this.        School.name, THIS.name, punishmentcontent)); }} public class Universityteacher:yteacher {public UniverSityteacher (int ID, String Name,yschool School): Base (ID, name, school) {}///<summa        Ry>///Hides a method of the parent class with the same name, hides the class only after it has been hidden and cannot access the method of the parent class. </summary> Public new void Toteachstudents () {Console.WriteLine (string. Format (@ "{0}" Teachers educate students: study hard. net! ", this.        Name)); }///<summary>///</summary> public override void Punishmentstude NTS (String punishmentcontent) {base.            Punishmentstudents (punishmentcontent);//You can also not execute the parent class method. Own code}}
Using System;namespace Yys. csharpstudy.mainconsole{    class program    {        static void Main (string[] args)        {            Universityteacher Uteacher = new Universityteacher (3, @ "Dong Bencheng", New Yschool (3, @ "Tsinghua University"));            The access is subclass method            uteacher.toteachstudents ();            You can access the overlay after the method            uteacher.punishmentstudents (@ "Let you hang the section.") ");            Yteacher teacher = new Universityteacher (3, @ "Dong Bencheng", New Yschool (3, @ "Tsinghua University"));            No access to the hidden method of            teacher. Toteachstudents ();            You can access the overridden method            teacher. Punishmentstudents (@ "Run 10000 meters. ");            Console.readkey ();}}}    

Results:

The subclass inherits the method of the parent class, and the process of a new method with the same name as the argument list is called hiding, using new to decorate a method of the same as the half. That is, the subclass hides the method of the parent class. However, hiding is different from overwriting, the hidden method can only be accessed by the class in which the method resides, and if a variable of the parent class is used, the hidden method is still accessed.
From the code above you can see the difference between covering and hiding. When a parent class variable references a subclass instance, it can only access the method that is hidden and cannot access the hidden method. However, you can access the post-overwrite method.
Another point is that if you want this method to cover the quilt class, then the parent class must add virtual to the method. Methods to hide the parent class The New keyword can also be added without. Hiding generally uses less, in some special cases to solve some problems.

The above is the basic knowledge of C #: Basic Knowledge (7) method of the hidden 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.