I. Types of inheritance
In object-oriented programming, there are two distinct types of inheritance: implementation inheritance and interface inheritance
1. Implementing Inheritance and Interface inheritance
* Implementation inheritance: Represents a type that derives from the base type and that owns all member fields and functions of that basis type. In implementation inheritance, the derived type takes the implementation code of each function of the base type, unless the implementation code of a function is specified in the definition of a derived type. You can use this type of inheritance when you need to add functionality to an existing type, or if many of the related types share a significant set of common functionality.
* Interface Inheritance: Indicates that a type inherits only the signature of a function and does not inherit any code. This type of inheritance is best used when you need to specify that the type has some of the available attributes.
2. Multiple Inheritance
C # does not support multiple inheritance, but C # allows a type to derive from multiple interfaces ———— multi-interface inheritance. This means that a C # class can derive from another class and any number of interfaces. More precisely, because System.Object is a common base class, each C # (except object) has a base class and can have any number of interfaces.
3. Inheritance of Structures
one limitation of using structs is that structs do not support implementation inheritance, but each structure is automatically derived from System.ValueType. Structs that implement type hierarchies cannot be encoded, but structs can implement interfaces.
Two. Implementation of inheritance
Syntax:
class Mydreved:baseclass
{
}
if a class or struct is also derived from an interface, the base class and interface in the list are separated by commas:
class Mydreved:baseclass,iintenface1,iintenface2
{
}
if the base class is not specified in the class definition, the C # compiler assumes that System.Object is a base class.
1. Virtual Methods
By declaring a base class function as virtual, you can override (override) The function in any derived class:
class BaseClass
{
Public virtual void Virtualmethod ()
{
//
}
}
You can also declare attributes as virtual. For virtual or overridden properties, the syntax is the same as a non-virtual property, but the virtual keyword is added to the definition:
Public virtual String Name
{
Get;set;
}
The concept of virtual functions in C # is the same as standard OOP: You can override virtual functions in derived classes. When the method is called, the appropriate method of the derived class is called. In C #, functions are not virtual by default, but (except constructors) can be explicitly declared as virtual.
When overriding a function in a derived class, use the override keyword to display the declaration:
class Mydreved:baseclass
{
Public override void Virtualmethod ()
{
//
}
}
neither the member field nor the static function can be declared as virtual, because the concept is meaningful only for instance function members in the class.
2. Hiding methods
if a method with the same signature is declared in both the base class and the derived class, but the method is not declared separately as virtual and override, the derived class method hides the base class method.
classA { Public voidA () {Console.WriteLine ('CLASS is A'); } }classb:a{ Public voidA () {Console.WriteLine ('CLASS is B'); }}classClient {Static voidMain () {b b=NewB (); A A=b; A.A (); B.A (); }}/*Output class is AClass is B*/
in most cases, the method is overridden rather than hidden, because the hidden method causes the wrong method to be called for the instance of the given class. However, C # syntax will receive a warning of this potential error at compile time.
In C #, to hide a method should be declared with the new keyword, so that no warning is issued at compile time:
class A
{
Public Void A ()
{
Console.WriteLine (' CLASS is A ');
}
}
class B:a
{
Public new Void A ()
{
Console.WriteLine (' CLASS is B ');
}
}
3. Calling the base class version of a function
C # can call the base version of a method from a derived class:base.<methodname> ()
class Mydreved:baseclass
{
Public override void Virtualmethod ()
{
base. Virtualmethod ();
}
}
You can use the base.<methodname> () syntax to call any method in the base class without calling it from an overload of the same method.
4. Abstract classes and abstract functions
C # allows classes and functions to be declared abstract. Abstract classes cannot be instantiated, and abstractions cannot be implemented directly, and must be overridden in non-abstract derived classes. Obviously the abstract function is also virtual (although it is not necessary to provide virtual, in fact, it cannot be provided).
If a class contains abstract functions, the class is also abstract and must be declared abstract:
Abstract class Building
{
Public abstract void Cal ();
}
a non-abstract method cannot be declared in an abstract class, but other non-abstract members can be declared.
5. Sealing type and Sealing method
C # allows classes and methods to be declared as sealed. For a class, this means that the class cannot be inherited, and for a method, the method cannot be overridden.
Sealed class A
{
}
class B:a//Error
{
}
If you do not want to have overridden methods and properties on the base class, do not declare it as virtual.
6. Constructors for derived classes
assuming that no displayed constructors are defined for any class, the compiler will provide a default initialization constructor for all classes, and in the background the compiler can solve the problem in the hierarchy of classes well, and each field in each class will be initialized to the corresponding default value.
When you create an instance of a derived class, you actually have multiple constructors that work. The constructor of the class to instantiate cannot itself initialize the class, and the constructor in the base class must also be called.
the order in which the constructors are called is to call object first, calling the constructor of the base class in accordance with the hierarchy, from the base class to the parent class, until the class to be instantiated is reached. In this procedure, each constructor initializes the fields in its own class. Because the constructor of the base class is always called first, the derived class can access the members of any base class during execution because the base class is already constructed and its fields are initialized.
* Add parameterless constructors in the hierarchy
adding a parameterless constructor to the hierarchy replaces the default constructor, so the parameterless constructor added in the base class is called by default during execution. Other aspects are unchanged.
* Add a constructor with parameters in the hierarchy
to call this constructor with parameters in the hierarchy, you need to display the call in the constructor of the parent class:
Public Abstract classgenericcustomer{Private stringname; PublicGenericcustomer () {name="<no name>"; } PublicGenericcustomer (stringname) { This. Name =name; } Public stringName {Get{returnname;} Set{name =value;} }} Public classnevermore60customer:genericcustomer{Private stringReferrername; Private UINThighcostminutesused; Ublic Nevermore60customer (stringName): This(Name,"<None>") { } PublicNevermore60customer (stringNamestringReferrername):Base(name) { This. Referrername =Referrername; } Public stringReferrername {Get{returnReferrername;} Set{Referrername =value;} }}
three. Modifiers
modifier To specify the visibility of a method, such as public or private, you can also specify the nature of an item, such as whether the method is virtual or abstract.
1. Visibility modifier
modifiers apply to the description
Public All classes and members any code can access
the members and inline classes of the protected class are accessible only within the class and within the derived class
internal all classes and members are accessible only inside the class and within the assembly that contains it
private class members and inline classes are accessed only within the class
the members and inline classes of the protected internal class are accessible only within the class, in the derived class, and in the assembly that contains it
classes cannot be defined as protected,private,protected internal, because these modifiers do not make sense for types that are contained in namespaces. Therefore, these modifiers can only be applied to members. However, you can use these modifiers to define nested classes (inline classes, classes that are contained in other classes), because in this case, the class also has the state of the member:
Public class Outerclass
{
protected Class Innerclass
{
}
}
2. Other modifiers
modifiers apply to the description
new function Hide function
static for all members
virtual function members can be overridden by derived classes
abstract class, function abstraction
the override function overrides the virtual and abstract members
sealed class, method, property cannot inherit and override
extern only static method members are externally implemented in another language
Four. Interface
Public Interface IDisposable
{
void Dispose ();
}
declaring an interface is exactly the same syntax as declaring an abstract class, but does not allow the implementation of any member. An abstract class can provide an implementation of a member other than a method, such as a property.
In general, interfaces can only contain declarations of methods, properties, indexers, and events.
interfaces cannot be instantiated, and interfaces cannot have constructors or fields. The interface definition also does not allow the inclusion of operator overloads.
It is not allowed to declare modifiers about members in an interface. Interface members are always public and cannot be declared as virtual and static. Declared in the implemented class, if desired.
the class that implements the interface must implement all members of the interface.
interfaces can inherit from each other in the same way that classes are inherited.
Inheritance of C #