Home>

inherit

A class can inherit from another class.In c#, there is only a single inheritance between classes.That is,There can be only one direct base class of a class.When implementing inheritance between classes,A subclass can treat all members of its direct base class as its own members,In addition to the static constructor, instance constructor, and destructor of the class.However, although all members of the base class can be treated as members of the subclass,But if members of the base class set different access permissions,The members that the derived class can access are also different.C#inheritance 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,All members of class a are also inherited (except the static constructor, instance constructor, and destructor of each base class). Subclasses (derived classes) can add their own members on the basis of inheritance,But it cannot remove members of the inherited parent (base) class. The role of the destructor is to destroy the instance of the class,I will summarize and explain in subsequent articles.

Look at the code example:

using system;
namespace lycheetest {
  public class tv {
    private int channel=1;//TV channel
    private int volume=20;//TV volume
    public static string model="39-inch LCD";//model
    ///<summary>
    ///Set the channel and volume of the TV specifically,Because it is only provided to subclasses, ///So it is decorated with protected access keywords
    ///</summary>
    ///<param name="ch">the number of channels specifically set</param>///¶m name="vol">the volume value</param>
    protected void set (int ch, int vol) {
      channel=ch;
      volume=vol;
      console.writeline ("Setting completed");
    }
    ///<summary>
    ///add 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 ("TV 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 ("Now start playing the video file of the USB flash 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,Line 3 defines the base class tv. Its static field and instance field both have an initializer to initialize the field.Line 11 adds an instance method,Its access modifier is protected. Use 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 intended for external use by the class.That is,It is not necessary to make it public to users.But its inherited classes need to use it,So using this access keyword can guarantee a certain level of publicity,Directed public,Only open for inherited classes.The purpose of this method is to specifically set the value of the instance field.Let the instance field simulate the content of the USB flash drive,The TV channel and volume can have a specific value.In addition,The other methods of the base class are unchanged. Line 37 defines a subclass,That is, derived classes.It inherits the syntax of the base class by adding a colon after the class name.Then pick the class name of the base class.Line 38 defines a method,In this method, the set method of the base class is called, and two parameters are passed to the method of the base class. These two parameters determine that when the content of the u disk is played,The TV channel is 0 and the volume is 30. Note that when calling the set method, The this keyword is used, which means that the method is the instance itself,Because it inherits from the base class,Equivalent to own property. Then call the show method of the base class to display the channel and volume settings again.Therefore, the relationship between the tv class and the newtv class can be described as follows,The tv-like can be seen as a prototype of a television,The newtv class can be seen as the basis of this prototype.The TV was upgraded again,It adds the function of USB flash disk playback,And other functions can be inherited directly from the prototype,No need to redesign.Line 46 defines an instance of the subclass,Then lines 47, 48, and 49 directly call the instance methods defined in the base class, Because these methods have been inherited,It belongs completely to the subclass itself.Line 50 calls the newly added own method defined by the subclass.Implementation of this code

The line results are as follows:

The TV model is:39-inch LCD
Channel:2
Volume:21 is set
The TV model is:39-inch LCD
Channel:0 Volume:30
Video files from the USB flash drive will now play.
.....
c
  • Previous C # SqlHelper application development learning
  • Next Android custom controls implement the bottom menu (below)