Home > Backend Development > C#.Net Tutorial > Sample code that briefly introduces inheritance of C# classes

Sample code that briefly introduces inheritance of C# classes

黄舟
Release: 2017-03-28 11:21:30
Original
1339 people have browsed it

This article mainly introduces the inheritance related knowledge of the C# class. It has a very good reference value. Let’s take a look at it with the editor.

Inheritance

A class can inherit from another class. In C#, there is only single inheritance between classes. In other words, a class can only have one direct base class. When inheritance is implemented between classes, the subclass can treat all members of its direct base class as its own members, except for the staticconstructor method and instance constructor method of the class. and Destruction method. However, although all members of the base class can be used as members of the subclass, if the members of the base class have different access permissions, the members that the derived class can access will also be different. Inheritance in C# is transitive. If class C is derived from class B, and class B is derived from class A, then class C will inherit all members of class B and also inherit all members of class A (static constructors of each base class , except instance construction methods and destructor methods). A subclass (derived class) can add its own members on the basis of inheritance, but it cannot remove inherited members of the parent class (base class). The function of the destructor method is to destroy instances of the class. I will summarize the explanation in subsequent articles.

Look at the code example below:

using System;
namespace LycheeTest {
  public class TV {
    private int channel = 1; //电视机的频道
    private int volume = 20; //电视机的音量
    public static string model = "39 英寸液晶"; //型号
    /// <summary>
    /// 具体设置电视机的频道和音量,因为只提供给子类使用  /// 所以用 protected 访问权限关键字修饰
    /// </summary>
    /// <param name="ch">具体设置的频道数</param>/// <param name="vol">具体设置的音量值</param>
    protected void Set(int ch, int vol) {
      channel = ch;
      volume = vol;
      Console.WriteLine("设置完毕");
    }
    /// <summary>
    /// 增加频道
    /// </summary>
    public void ChPlus() {
      channel++;
    }
    /// <summary>
    /// 增加音量
    /// </summary>
    public void VolPlus() {
      volume++;
    }
    /// <summary>
    /// 将信息显示在电视机屏幕上
    /// </summary>
    public void Show() {
      Console.WriteLine("电视机型号是:{0}", model);
      Console.WriteLine("频道:{0}", channel);
      Console.WriteLine("音量:{0}", volume);
    }
  }
  public class NewTV: TV {
    public void PlayUDisk() {
      this.Set(0, 30);
      this.Show();
      Console.WriteLine("现在开始播放 U 盘的视频文件......");
    }
  }
  class Program {
    static void Main(string[] args) {
      NewTV myNewTV = new NewTV();
      myNewTV.ChPlus();
      myNewTV.VolPlus();
      myNewTV.Show();
      myNewTV.PlayUDisk();
      Console.ReadKey();
    }
  }
}
Copy after login

In the above code, the 3rd line of code defines the base class TV. Its static fields and instance fields have an initializer to initialize the fields. Line 11 adds an instance method with the access modifier protected. Using this modifier, it can only be accessed within the definition of this class and within its derived classes. Why use this access modifier? Because this method is not for external use of the class. That is, it does not need to be exposed to users. But its inherited classes need to use it, so using this access permission keyword can ensure a certain degree of publicity, that is, directed disclosure, only open to inherited classes. The purpose of this method is to specifically set the value of the instance field. Let the instance field have a specific value for the channel and volume of the TV when simulating the content of the USB flash drive. Apart from this, the other methods of the base class have not been changed. Line 37 of code defines a subclass, also known as a derived class. The syntax for inheriting from a base class is to add a colon after the class name, followed by the class name of the base class. Line 38 of the code defines a method, in which the Set method of the base class is called, and two parameters are passed in to the base class method. These two parameters determine the TV when playing the content of the USB flash drive. The channel is 0 and the volume is 30. Note that when calling the Set method, the this keyword is used, which indicates that this method is the instance's own, because it is inherited from the base class and 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 TV-like and NewTV-like can be described as follows: TV-like can be seen as a prototype of a TV set, and NewTV-like can be seen as an upgraded TV set based on this prototype. It adds the function of USB disk playback, and other functions can be inherited directly from the prototype without having to redesign it. Line 46 of code defines an instance of the subclass, and then lines 47, 48, and 49 directly call the instance methods defined in the base class, because these methods have been inherited and belong entirely to the subclass itself. Line 50 calls the newly added method defined by the subclass. The execution result of this code is as follows:

电视机型号是:39 英寸液晶 
频道:2
音量:21 设置完毕
电视机型号是:39 英寸液晶
频道:0 音量:30
现在开始播放 U 盘的视频文件......
Copy after login

The above is the detailed content of Sample code that briefly introduces inheritance of C# classes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c#
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template