Basic knowledge of C #: Basics (4) Inheritance

Source: Internet
Author: User
Tags access properties
As mentioned earlier, object-oriented three major features: encapsulation, inheritance and polymorphism. Encapsulation in the definition of the previous class is almost thorough, now look at the inheritance characteristics.
Inheritance is actually an extension of a class to another class, which is called a base class, which is called a subclass. Inheritance is that subclasses have all the properties and methods of the base class, and subclasses can also add properties and methods. However, subclasses cannot remove the properties and methods of the parent class.
Of course, there is also the question of modifiers, which have all the properties and methods of the base class, and do not imply that subclasses can arbitrarily access the inherited properties and methods. Subclasses can only access properties and methods that are decorated with public and protected, and the rest cannot be accessed directly. Another is that static properties and methods cannot be inherited, because static types are related to objects regardless of their relationship to the class.
Look at the code:

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;            }}///<summary>//constructor///</summary> public Yschool () {            this.id = 0;        THIS.name = @ "Middle School of Tsinghua University";        }///<summary>//constructor///</summary> 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;        <summary>///Use a read-only attribute because it is an intrinsic property.            </summary> public int ID {get {return id;            }} public string name {get {return Name;        }}///<summary>////These use the Get/set attribute because these properties are not intrinsic and can be changed with some conditions.                </summary> 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;            }} public yteacher (int ID, string name) {this.id = ID;        THIS.name = name;             }///<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)); }}///<summary>//male teacher, inherit from YteaCher//</summary> public class Mrteacher:yteacher {///<summary>//constructor, note here that Yte        Acher is not provided with a default constructor,///So the subclass must have a constructor and be consistent with the base class parameter list.        </summary>//<param name= "id" ></param>//<param name= "name" ></param>        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));        }}///<summary>//female teacher, inherit from Yteacher///</summary> public class Misteacher:yteacher {        <summary>///constructor///</summary>//<param name= "id" ></param>        <param name= "name" ></param> public misteacher (int ID, string name): Base (ID, name) {}///<summary&Gt Extension methods, skincare methods///</summary> public void Skincare () {Console.WriteLine (string. Format (@ "{0}" the teacher uses Chanel skin cream for skincare. ", this.        Name)); }    }}
Using System;namespace Yys. csharpstudy.mainconsole{        class program    {        static void Main (string[] args)        {            Mrteacher mrteacher = New Mrteacher (1, @ "Bull Rumble");            Mrteacher.toteachstudents ();            Mrteacher.punishmentstudents (@ "Back tang poetry");            Mrteacher.shave ();            Misteacher misteacher = new Misteacher (2, @ "郝漂 Liang");            Misteacher.toteachstudents ();            Misteacher.punishmentstudents (@ "The dream of Red mansions in Dictation");            Misteacher.skincare ();            Console.readkey ();        }    }

Results:

Inheritance is an object-oriented feature, and its benefits are:
First, when doing project design class, inheritance can let us omit the volume code;
Second, in accordance with the organization of the object-oriented class, in C #, Java and other advanced object-oriented languages, there is an object class, which is the ancestor class of all classes, other classes have his derivation.
Inheritance needs to be aware that the constructor of a subclass is a problem. The program runs as a subclass that first calls the parent class's constructor. If the subclass does not have a write constructor, the parent class's default constructor is called. If the parent does not have a default constructor, that is, the parent writes the constructor with parameters, then the subclass calls the constructor with the parameter, but it needs to indicate which constructor (see base in code) is called.
The above is the basic knowledge of C #: Basic knowledge (4) inherited 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.