On the Inheritance of C Class

  • 2021-11-29 08:14:03
  • OfStack

Inheritance

1 class can inherit from another class. In C #, there is only single 1 inheritance between classes. That is, there can only be one direct base class for one class. When class-to-class inheritance is implemented, a subclass can treat all members of its immediate base class as its own, except for the static constructor, instance constructor, and destructor of the class. However, although all members of the base class can be treated as members of a subclass, if the members of the base class have different access permissions, the derived classes can access different members. The inheritance of C # is transitive, and if class C is derived from class B and class B is derived from class A, then class C inherits all members of class B as well as all members of class A (except static constructors, instance constructors, and destructors for each base class). A child class (derived class) can add its own members on the basis of inheritance, but it cannot remove the members of the inherited parent class (base class). The function of the destructor method is to destroy instances of classes, which I will summarize in subsequent articles.

Look at the code example below:


using System;
namespace LycheeTest {
  public class TV {
    private int channel = 1; // Channel of TV set 
    private int volume = 20; // Volume of TV set 
    public static string model = "39  Inch liquid crystal "; // Model 
    /// <summary>
    ///  Set the channel and volume of the TV specifically, because it is only provided for subclasses to use   ///  So use  protected  Access rights keyword modification 
    /// </summary>
    /// <param name="ch"> Number of channels specifically set </param>/// <param name="vol"> Specific set volume value </param>
    protected void Set(int ch, int vol) {
      channel = ch;
      volume = vol;
      Console.WriteLine(" Setup finished ");
    }
    /// <summary>
    ///  Increase channel 
    /// </summary>
    public void ChPlus() {
      channel++;
    }
    /// <summary>
    ///  Increase the volume 
    /// </summary>
    public void VolPlus() {
      volume++;
    }
    /// <summary>
    ///  Display information on the TV screen 
    /// </summary>
    public void Show() {
      Console.WriteLine(" The TV set model is: {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(" Start playing now  U  Video file of disk ......");
    }
  }
  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, line 3 defines the base class TV. Its static field and instance field have 1 initializer to initialize the field. Line 11 adds an instance method whose access modifier is protected. With this modifier, it is accessible only 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. In other words, it is not necessary to disclose it to users. However, its inheriting classes need to use it, so using this access keyword can guarantee a certain degree of openness, that is, directed disclosure, which is only open for inheriting 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 TV channel and volume when simulating the contents of the U disk. In addition, other methods of the base class have not changed. Line 37 defines a subclass, that 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. Line 38 defines a method in which the Set method of the base class is called, and two parameters are passed in to the method of the base class, which determine that the TV channel is 0 and the volume is 30 when playing the contents of the U disk. Note that when the Set method is called, the this keyword is used, which means that the method is the instance's own, because it inherits 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 class TV and class NewTV can be described as follows: class TV can be regarded as a prototype of a TV set, and class NewTV can be regarded as an upgrade of the TV set based on this prototype, adding the function of playing U disk, while other functions can be directly inherited from the prototype without redesign. Line 46 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 are inherited and belong entirely to the subclass itself. Line 50 calls the newly added method of the subclass definition. The execution of this code

The row results are as follows:


 The TV set model is: 39  Inch liquid crystal  
 Channel: 2
 Volume: 21  Setup finished 
 The TV set model is: 39  Inch liquid crystal 
 Channel: 0  Volume: 30
 Start playing now  U  Video file of disk ......

Related articles: