Sample code for inheritance of C # classes in brief

Source: Internet
Author: User
This article mainly introduces the inheritance related knowledge of C # class. Have a good reference value, follow the small series together to see it

Inherited

A class can inherit from another class. In C #, there is only a single inheritance between classes and classes. That is, the direct base class of a class can have only one. When inheriting between a class and a class, a subclass can treat all members of its immediate base class as its own members, in addition to the static construction methods of the class, the instance construction method, and the destructor method. However, although all members of a base class can be members of a subclass, the members that the derived class can access are also different if the members of the base class set different access rights. C # inheritance is transitive, if Class C derives from Class B, and Class B derives from Class A, then Class C inherits all the members of Class B, with the exception of the static constructor method, instance constructor method, and destructor for each base class. A subclass (derived class) can add its own members on the basis of inheritance, but it cannot remove the members of the inherited parent class (the base class). The function of the destructor is to destroy an instance of the class, and the subsequent article will summarize the description.

Here's a look at the code example:

Using System;namespace lycheetest {public class TV {private int channel = 1;//TV channel private int volume = 20;// The TV's volume public static string model = "39 inch LCD"; Model///<summary>///Set the channel and volume of the TV, because it is only available to subclass///So use protected access keyword to decorate///</summary>/&L T;param name= "ch" > number of channels specified </param>///<param name= "Vol" > Specific volume values </param> protected void set (int      CH, int vol) {channel = ch;      volume = Vol;    Console.WriteLine ("Setup Complete");    }///<summary>//Add channel///</summary> public void Chplus () {channel++;    }///<summary>///Increase volume///</summary> public void Volplus () {volume++; }///<summary>///display the information on the TV screen////</summary> public void Show () {Console.WriteLine ("TV model      Yes: {0} ", model);      Console.WriteLine ("Channel: {0}", channel);    Console.WriteLine ("Volume: {0}", volume); }} public class Newtv:tv {public void Playudisk () {     This.      Set (0, 30); This.      Show ();    Console.WriteLine ("Now start playing video files of the USB drive ...");      }} class Program {static void Main (string[] args) {newtv mynewtv = new NewTV ();      Mynewtv.chplus ();      Mynewtv.volplus ();      Mynewtv.show ();      Mynewtv.playudisk ();    Console.readkey (); }  }}

In the above code, the 3rd line of code defines the base class TV. Its static fields and instance fields have an initializer for the initialization of the field. The 11th line of code adds an instance method whose access modifier is protected. With this modifier, only inside the definition of this class and within its derived class can access it. Why use this access modifier? Because this method is not used outside of the class. That is, it does not need to be exposed to users. However, its inheriting class needs to use it, so using this access keyword can guarantee a certain degree of openness, that is, the orientation is open, only for the inheriting class. The function of this method is to set the value of the instance field. Lets the instance field have a specific value for the channel and volume of the TV when simulating the contents of the USB drive. In addition, other methods of the base class have not changed. The 37th line of code defines a subclass, which is a derived class. It inherits the syntax of the base class by adding a colon after the class name, followed by the class name of the base class. The 38th line of code defines a method in which the base class's Set method is called, and two parameters are passed to the base class method, which determines that the TV channel is 0 and the volume is 30 when the contents of the USB flash drive are played. Note that when the Set method is called, the This keyword is used, which means that the method is an instance of itself, because it inherits from the base class, equivalent to its own property. The base class's show method is then called to display the Channel and volume settings again. Thus, the relationship between class TV and class NewTV can be described as such, the class TV can be seen as a prototype of a television set, class NEWTV can be seen as the basis of this prototype, the TV has been upgraded, it added the function of the USB flash drive, and other functions can directly inherit from the prototype, and no need to re- Design. The 46th line of code defines an instance of the subclass, and then lines 47th, 48, and 49th call the instance methods defined in the base class directly, because these methods are inherited and belong entirely to the subclass itself. Line 50th calls the newly added method of the subclass definition that belongs to itself. This piece of code

The results are as follows:

TV model is: 39-inch LCD Channel: 2 Volume: 21 Set the TV model is: 39-inch LCD Channel: 0 Volume: 30 now start playing the video file of the USB stick ...
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.