Inheritance is divided into two parts: interface inheritance and implementation inheritance.
Implementation inheritance: Represents a new class derived from the base class, which has all of the functions and fields (not accessible to the base class private functions and fields) that are in the basis class.
Interface Inheritance: Indicates that a type inherits only the signature of a function, does not inherit any implementation code, and is typically used when it has a particular property.
Virtual (virtual method)
If the inheriting class wants to override the method of the base class, the method of the base class can be written as a virtual method, with the Override keyword overridden in the inheriting class.
Abstraction (abstract methods and abstract classes)
Abstract classes cannot be instantiated, functions in abstract classes, only declarations of functions, no specific implementation code, must be overridden in subclasses, overriding the function with the override keyword. Abstract functions can only be defined in abstract classes.
The difference and connection between virtual and abstract
Derived class constructors: in the hierarchy of hierarchical classes (multi-level inheritance), the order of instantiation is to instantiate the underlying base class first, and then instantiate the subclass in turn until the class to instantiate.
Extension type: If a known class (Personclass ) has no source code and cannot implement overrides and inheritance, you can extend the class with an extension type, create a static class, and add a static method.
Public static Class Newperson
{
public static string Sayname (this person per,string Name)
{
Specific implementation
}
}
The This keyword in the Newperson class indicates the type of the class to extend the person type, in which the methods and properties of the person class can be accessed. This allows you to directly access the Sayname function when defining the person class.
Person P=new person ();
P.sayname ("text")
C # Base inheritance and interfaces, extension types