C # core Foundation-class inheritance,

Source: Internet
Author: User

C # core Foundation-class inheritance,
Inheritance

  A class can inherit from another class. In C #, only one inheritance exists between classes. That is to say, a direct base class of a class can only have one. When inheritance is implemented between a class and a class, the subclass can treat all the members of its direct base class as its own members, besides static constructor, instance constructor, and destructor. However, although all the members of the base class can be used as sub-class members, if the base class members set different access permissions, the members that can be accessed by the derived class are also different. C # inheritance can be passed. If Class C is derived from class B and class B is generated from Class A, Class C will inherit all the members of Class B, it also inherits all the members of Class A (except static constructor, instance constructor, and destructor of each base class ). A subclass (derived class) can add its own members based on the inheritance, but it cannot remove the inherited members of the parent class (base class. The role of the Destructor is to destroy the instance of the class. I will summarize and describe it in subsequent articles.

See the code example below:

1 using System; 2 namespace LycheeTest {3 public class TV {4 private int channel = 1; // TV channel 5 private int volume = 20; // TV volume 6 public static string model = "39 inch LCD"; // model 7 // <summary> 8 // set the TV channel and volume, because it is only available to subclass users, use the keyword protected to modify 9 // </summary> 10 /// <param name = "ch"> specific channel number </param> /// <param name = "vol"> specific volume value </param> 11 protected void Set (int ch, int vol) {12 channel = ch; 13 volume = vol; 14 Console. writeLine ("set up"); 15} 16 // <summary> 17 // Add channel 18 /// </summary> 19 public void ChPlus () {20 channel ++; 21} 22 // <summary> 23 // increase the volume by 24 /// </summary> 25 public void VolPlus () {26 volume ++; 27} 28 // <summary> 29 // display the information on the TV screen 30 /// </summary> 31 public void Show () {32 Console. writeLine ("TV model: {0}", model); 33 Console. writeLine ("channel: {0}", channel); 34 Console. writeLine ("volume: {0}", volume); 35} 36} 37 public class NewTV: TV {38 public void PlayUDisk () {39 this. set (0, 30); 40 this. show (); 41 Console. writeLine ("start playing the video file of the U disk now ...... "); 42} 43} 44 class Program {45 static void Main (string [] args) {46 NewTV myNewTV = new NewTV (); 47 myNewTV. chPlus (); 48 myNewTV. volPlus (); 49 myNewTV. show (); 50 myNewTV. playUDisk (); 51 Console. readKey (); 52} 53} 54}

In the above Code, the first line of code defines the base class TV. Its static fields and instance fields both have an initial value setting item for field initialization. An instance method is added to the Code in line 11th. Its access modifier is protected. With this modifier, only the class definition and its derived class can access it. Why is this access modifier used? This method is not used outside the class. That is to say, it does not need to be made public to users. However, its inheritance class needs to use it. Therefore, using this access permission keyword can ensure a certain degree of openness, that is, it is only open to the inheritance class. This method is used to set the value of the Instance field. When the instance field is used to simulate playing the content of the U disk, the TV channel and sound volume can have a specific value. In addition, other methods of the base class are not modified. The Code in line 37th defines a subclass, that is, a derived class. The syntax that inherits the base class is to add a colon after the class name, followed by a base class name. The Code in line 38th defines a method. In this method, the Set Method of the base class is called, and two parameters are input for the base class method, these two parameters determine that when playing the content of the U disk, the TV channel is 0 and the volume is 30. Note that when the Set method is called, this keyword is used, indicating that this method is the instance's own, because it inherits from the base class, which is equivalent to its own property. Then, the Show method of the base class is called to display the channel and volume settings again. Therefore, the relationship between the class TV and the class NewTV can be described in this way. The class TV can be seen as a TV prototype, and the class NewTV can be seen as the basis of this prototype, the TV has been upgraded, and the U disk playing function has been added. Other functions can be inherited directly from the prototype without having to be re-designed. The first line of code defines the subclass instance, and then the second line,48 The row and row 49th directly call the instance methods defined in the base class, because these methods have been inherited and belong to the subclass itself. Row 50th calls the newly added method defined by the subclass. The execution result of this Code is as follows:

TV model: 39 inch LCD channel: 2 volume: 21 set the TV model is: 39 inch LCD channel: 0 volume: 30 now play U disk video files ......

 

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.