C# inheritance knowledge points

Inheritance
1. Definition
Why: Solve the problem of duplication of functions What: A class has the attributes and methods of another class. This class is called a subclass (derived class), and the other class is called the key symbol inherited by
the parent class (base class, super class)
: (colon)
features:
1) The subclass inherits the properties and methods (public, protected) of the parent class, but the construction method cannot be inherited
2) The subclass uses the construction method to call the parent class construction method and uses base()
3) Create a subclass When class objects, the system defaults to call the parent class construction method first, and then call the subclass construction method
4) The subclass uses the properties and methods of the parent class with the base keyword (public, protected)
Note: when the parent class has a parameterized construction method , it is required that the parent class must have a parameterless construction method or a subclass construction method to call the parent class's parameterized construction method.
Benefits:
1) Inheritance is the most effective way to construct, establish and expand new classes on the basis of some more general classes . (Expand new functions)
2) Inheritance simplifies people's understanding and description of things, and can clearly reflect the hierarchical relationship between related classes.
3) Inheritance provides a software reuse function. If class B inherits class A, then when creating class B, it only needs to describe a small number of features (data members and member methods) that are different from the base class (class A). This approach can reduce the redundancy of code and data, greatly increasing the reusability of the program.
4) Inheritance reduces interfaces and interfaces between modules by enhancing consistency, which greatly increases the maintainability of the program.

2. Override Override
: In the subclass and parent class, the method name in the subclass is the same as the method name of the parent class, and the parameters are the same. Overload
: In the same class, the method name is the same, and the parameters are the same. Different (number, type), has nothing to do with the return value

3. Virtual method (virtual)
A member method that declares virtual in a base class and is redefined in one or more derived classes is called a virtual method.
By declaring the keyword virtual in the function of the base class, the method can be overridden by the keyword override in its derived class. The rewritten virtual method is still a virtual method.

4. The sealed keyword
C# provides a sealed modifier, which prevents other classes from inheriting from this class.

5. Exercises
Realize the inheritance relationship between the following classes, and write the Music class to test these classes
insert image description here

class Instrument
  {
    
    
    public virtual void Play()//虚方法
    {
    
    
     Console.WriteLine("弹奏xxx!");
    }
   }
class Wind:Instrument
 {
    
    
   public override void Play()//重写方法
    {
    
    
     Console.WriteLine("弹奏wind!");
    }
   public void Play2()
    {
    
    
     Console.WriteLine("调用wind的play2");
     }
  }
class Brass:Instrument
  {
    
    
   public override void Play()//重写方法
    {
    
    
     Console.WriteLine("弹奏Brass!");
    }
   public void Play2()
    {
    
    
      Console.WriteLine("调用brass的play2");
    }
}

Guess you like

Origin blog.csdn.net/weixin_44706943/article/details/125958659