C # Inheritance (3) Continuous update

Source: Internet
Author: User

Class inheritance and interface inheritance

A class inherits a type derived from a base class row that owns all member fields and functions of that base type.

The interface inherits the signature of a type-inherited function and does not require implementation code.

Multiple inheritance

A class derives from more than one class. Multiple inheritance writing is very complex and difficult to maintain. Therefore, C # does not support multiple inheritance. But can be derived from multiple interfaces, multiple interface inheritance.

Structures and Classes

Structures (value types) and classes (reference types). One limitation of using structs is that structs do not support inheritance, and each structure is automatically derived from System.ValueType. Structs cannot implement type inheritance, but implement interface inheritance.

    • Structs are always derived from System.ValueType, and they can derive from any number of interfaces.
    • Classes are always derived from syste.object or another class that the user chooses, and they can also derive from any number of interfaces.
Implementing inheritance
 Public class money:mybaseclass{}

C + + supports the concept of public inheritance, private inheritance, and note that C # does not support private inheritance. Therefore, there are no public and private qualifiers on the class name, and private inheritance only adds complexity, so C + + also uses very little.

Derived classes and interfaces

 Public class Money:mybaseclass, IInterface1, iinterface2{}

Structure

 Public struct Money:iinterface1, iinterface2{}
Virtual method

The concept of C # virtual functions is the same as the concept of standard OOP: You can override a virtual function in a derived class. In addition to constructors, you can explicitly declare virtual. In Java, all functions are virtual functions.

 Public class mybaseclass{    publicvirtualvoid  Virtualmethod ()    {            }}  Public class money:mybaseclass{    publicoverridevoid  Virtualmethod ()    {        base. Virtualmethod ();    }}

Neither the member field nor the static function can be declared as virtual.

Hide method

The same method of signing 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. Use the new key to hide the method.

 Public class mybaseclass{    publicnewvoid  Virtualmethod ()    {            }}  publicclass  money:mybaseclass{    publicvoid   Virtualmethod ()    {           }}
Call the base class method
base. Virtualmethod ();
Abstract classes and abstract functions

C # allows classes and functions to be declared abstract. Abstract classes cannot be instantiated, and abstract functions cannot be implemented directly, and must be overridden in non-abstract derived classes. The abstract function itself is virtual, so it cannot be added with the Vsan keyword.

Abstract functions in C + + are called pure virtual functions. C # is called an abstract function.

Abstract  Public class mybaseclass{    publicabstractvoid  Virtualmethod ();}  Public class money:mybaseclass{    publicoverridevoid  Virtualmethod ()    {           }}
Sealing class and Sealing method

Sealed

    • Class indicates that the class cannot be inherited
    • Method indicates that the method cannot be overridden
Sealed  Public class mybaseclass{    publicvoid  Virtualmethod ()    {            }}

A string is a sealed class. Sealed can also be used in overriding functions. Prevent others from rewriting functions.

Constructors for derived classes

The execution order of the constructor is performed from the top down, and the constructor of the base class is always called first.

To add a parameterless constructor in a hierarchy

 Public Abstract class mybaseclass{    privatestring  name;      Public Base ()    {        "<no name>";    }}

C # Inheritance (3) Continuous update

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.