C # inheritance,

Source: Internet
Author: User

C # inheritance,

1. inherited types
In object-oriented programming, there are two completely different inheritance types: Implementation inheritance and interface inheritance.
  1. Implement inheritance and interface inheritance
* Implement inheritance: indicates that a type is derived from the base type and has all member fields and functions of the base type. In implementation inheritance, the derived type uses the implementation code of each function of the base type unless the implementation code of a function is specified in the definition of the derived type. You can use this type of inheritance when you need to add a function to an existing type or many related types share an important set of public functions.
* Interface inheritance: indicates that a type only inherits the function signature and does not inherit any code. When you need to specify that this type has certain available features, it is best to use this type of inheritance.
  2. Multi-Inheritance
C # does not support multi-inheritance, but C # allows the type to be derived from multiple interfaces-multi-interface inheritance. This indicates that the C # class can be derived from another class and any number of interfaces. More accurately, because System. Object is a common base class, each C # (except the Object) has a base class and can have any number of interfaces.
  3. Structure Inheritance
One restriction of the structure is that the structure does not support inheritance, but each structure is automatically derived from System. ValueType. It cannot be encoded to implement a type-level structure, but the structure can implement interfaces.

Ii. Inheritance implementation
Syntax:
Class MyDreved: BaseClass
{

}
If the class or structure is also derived from the interface, use commas to separate the base class and interface in the list:
Class MyDreved: BaseClass, IIntenface1, IIntenface2
{

}

If no base class is specified in the class definition, the C # compiler assumes that System. Object is a base class.

  1. Virtual Methods
To declare a base class function as virtual, You can override the function in any derived class:
Class BaseClass
{
Public virtual void VirtualMethod ()
{
//
}
}

You can also declare the attribute as virtual. For virtual or overwrite attributes, the syntax is the same as that for non-virtual attributes, but you must add the virtual keyword to the definition:
Public virtual string Name
{
Get; set;
}

The concept of virtual functions in C # is the same as that of standard OOP: virtual functions can be rewritten in a derived class. When you call a method, 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 you override a function in a derived class, you must 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 this concept only makes sense to the instance function members in the class.

2. Hide Methods
If the method with the same signature is declared in both the base class and the derived class, but this method is not declared as virtual and override, the derived class method will hide the base class method.

Class A {public void a () {Console. writeLine ('class is A');} class B: A {public void A () {Console. writeLine ('class is B ') ;}} CLASS client {static void main () {B B = new B (); A a = B;. a (); B. a () ;}}/* output class is aclass is B */

 

In most cases, it is necessary to rewrite the method, rather than hide the method, because hiding the method will cause an error in calling the method for the instance of the given class. However, the C # syntax will receive a warning of this potential error during compilation.

In C #, to hide a method, the new Keyword declaration should be used, so that no warning will be issued during compilation:
Class
{
Public void ()
{
Console. WriteLine ('class is ');
}
}

Class B:
{
Public new void ()
{
Console. WriteLine ('class is B ');
}
}

3. base class version of the called Function
C # basic version of the method that can be called from the 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 the same method overload.

4. abstract classes and Abstract Functions
C # the class and function can be declared as abstract. The abstract class cannot be instantiated, but the abstract cannot be implemented directly. It must be rewritten in a non-abstract derived class. Obviously, abstract functions are also virtual (although virtual is not required, this keyword is not actually available ).
If a class contains an abstract function, the class is also abstract and must be declared as abstract:
Abstract class Building
{
Public abstract void Cal ();
}

Abstract classes cannot declare non-abstract methods, but can declare other non-Abstract members.

  5. sealing and sealing methods
C # declare classes and methods as sealed. For classes, this indicates that the class cannot be inherited; for methods, this indicates that the method cannot be rewritten.
Sealed class
{

}

Class B: A // Error
{

}

If you do not want override methods and attributes on the base class, do not declare them as virtual.

6. constructor of the derived class
Assuming that no displayed constructor is defined for any class, the compiler will provide default initialization constructor for all classes. In the background, the compiler can effectively solve the problems in the class hierarchy, each field in each class is initialized to the corresponding default value.
When creating an instance of a derived class, multiple constructors actually work. The constructor of the class to be instantiated Cannot initialize the class, but must also call the constructor in the base class.
The Calling sequence of constructor is to call the Object first. In the hierarchy, the base class constructor is called from the base class to the parent class until it reaches the class to be instantiated. In this process, each constructor initializes fields in its own class. Because the base class constructor is always called first, the derived class can access any base class member during execution, because the base class has been constructed and Its fields have also been initialized.

  * Add a constructor without parameters to the hierarchy.
Adding a non-parameter constructor to the hierarchy replaces the default constructor. Therefore, the non-parameter constructor added to the base class is called by default during execution. Other aspects remain unchanged.
  * Add a constructor with parameters to the hierarchy.
To call this constructor with parameters in the hierarchy, you must display the call in the constructor of the parent class:

public abstract class GenericCustomer{    private string name;    public GenericCustomer()    {        name = "<no name>";    }    public GenericCustomer(string name)    {        this.name = name;    }    public string Name     {         get {return name;}        set {name = value;}    }}public class Nevermore60Customer : GenericCustomer{    private string referrerName;    private uint highCostMinutesUsed;    ublic Nevermore60Customer(string name) : this(name, "            <None>")    {    }    public Nevermore60Customer(string name, string referrerName) : base(name)    {        this.referrerName = referrerName;    }    public string ReferrerName    {        get {return referrerName;}         set {referrerName = value;}    }}    

 

 

 

3. Modifier
Modifier can specify the visibility of a method, such as public or private, or specify the essence of an item. For example, the method is virtual or abstract.
  1. Visibility Modifier
Modifier applied to description
Public all classes and Members can access any code
Members and embedded classes of the protected class can only be accessed within the class and in the derived class.
All classes and members of internal can only be accessed within and within the class.
Private class members and embedded classes can only be accessed within the class
Members of the protected internal class and embedded classes can only be accessed within the class. In the derived class, they are centrally accessed by the programs that contain it.

Classes cannot be defined as protected, private, or protected internal, because these modifiers do not make sense for the types contained in the namespace. Therefore, these modifiers can only be applied to Members. However, you can use these modifiers to define Nested classes (embedded classes, including classes in other classes), because in this case, classes also have Member States:
Public class OuterClass
{
Protected class InnerClass
{

}
}

2. Other Modifiers
Modifier applied to description
New Function hidden function
Static all members static
A virtual function member can be rewritten by a derived class.
Abstract class, function Abstraction
Override function override virtual and abstract members
Sealed class, method, and attribute cannot be inherited or overwritten
Extern is implemented externally only by static method members in another language.

Iv. Interface
Public interface IDisposable
{
Void Dispose ();
}

The Declaration interface is similar to the declaration abstract class in syntax, but it cannot provide any member implementation method. Abstract classes can be implemented by other members except methods, such as attributes.
Generally, an interface can only contain methods, attributes, indexers, and event declarations.
The interface cannot be instantiated. The interface cannot have constructors or fields. The interface definition cannot contain Operator overloading.
The modifier about Members cannot be declared in the interface. Interface members are always public and cannot be declared as virtual or static. If necessary, declare it in the implemented class.

The class that implements the interface must implement all the members of the interface.
Interfaces can inherit from each other in the same way as classes.

Related Article

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.