"C # Advanced Programming", "fourth chapter" Inheritance--study notes

Source: Internet
Author: User
Tags naming convention

computer programs, to a large extent, are designed to describe and solve real-world problems. In object-oriented language, the class is very good to adopt the method of abstraction and classification in human thought, and the relation between the class and the object reflects the relation of the individual and the common characteristic of the similar group well. But there are still a few differences between the many things in common. Therefore, the inheritance mechanism is designed in object-oriented language, which allows us to expand it based on preserving the original class characteristics. Because of the introduction of the inheritance and derivation mechanism of the class, the reusability and extensibility of the code are greatly improved. With this mechanism, we can still stand on the shoulders of giants. Development---The use of other written classes to expand, which can improve our development efficiency. In the process of deriving a new class there are generally three steps: absorbing the base class member, transforming the base class member, adding a new member.
With the concept of inheritance, let's look at what types of inheritance are in C # (not much more direct).

After a cursory understanding of C # 's inheritance types, let's look at it in detail.
Inheritance in C # defaults to public inheritance, so you do not need to add qualifiers before the base class name, which is somewhat different from C + +. There are three ways of inheriting (public, protected, private) in C + +, which is much more concise than C # 's inheritance. Now let's look at how this is inherited, with the following syntax:
① inheritance between class and class

[Access modifier] class [custom class name]: [Base class name]{//class body}

② inheritance between a class (struct) and an interface

[Access modifier] class [custom class name]: [Interface 1 Name],[Interface 2 name]{//class Body}

(if it is a struct, it becomes a struct-inheriting interface if the keyword class is changed to a struct)
③ Hybrid Inheritance

[Access modifier] class [custom class name]: [base class name],  [Name of interface 1],[Interface 2 name]{//class Body}

( Note : If the base class is not specified in the class definition, the C # compiler and the assumed System.Object are the base class, which can be abbreviated as Object)


We said earlier that there are three steps to deriving a new class, so let's start by saying:
1, for the absorption of the base class members, is actually the base class inherited members do not make any changes.
2, for the transformation of the base class Members:
The way the original class method is handled is inappropriate for the new class, and we need to transform the inherited method in the new class. And according to the different ways of transformation can be divided into: virtual methods and hidden methods.
<1> virtual method
The method of inheriting from the base class is modified (that is, rewritten).
Concrete steps: First declare the function of the base class as virtual, the syntax is as follows:

[Access modifier] virtual [function return value type] Function name (parameter table) {//function body}

In the subclass, when overriding the function, use the Override keyword with the following syntax:

[Access modifier] override [function return value type] Function name (parameter table) {//overridden function body}

( note : Neither the Member field nor the static function can be declared as virtual)
<2> hiding methods

The hidden method does not actually make a real transformation, but a method with the same name as the original method is redefined in the subclass. Then hide the inherited method. The syntax is as follows:

[Access modifier] new [function return value type] Function name (parameter table) {//overridden function body}

Now let's see what the difference is between the two ways of transformation. For example, the method we are going to inherit is a. So it is simple to understand that in a subclass, the virtual method actually removes the inherited method A, and then replaces the original method A with the newly defined method a in the subclass. In other words, when a virtual method (override) is used, there is only one method A in the subclass (this method A is what we rewrite). With the above logic we can get: when using the Hidden method, there are two method A in the subclass (one is inherited from the parent class and one is newly defined in the subclass). So, in the process of invoking method A, when we are going up, we call the subclass method A for the rewrite. For a hidden method, we will call method A of the parent class.

3, add a new member, this step is to extend the original class, the way to add members and ordinary class no difference.


In the new class, we sometimes need to invoke the base class version of the method, syntax:base.< method name > (parameter list)
( Note : This syntax can call any method in the base class without having to invoke it from the same method's overloads)

Abstract classes and abstract functions, this is a bit of a bad explanation. We take cars for example: Abstract classes are equivalent to the properties common to all cars. Inherit these properties and extend them, then we can get all kinds of cars. Abstract functions, however, are equivalent to the properties of a car. Declares abstract classes and abstract functions using abstract.
( note : Abstract classes cannot be instantiated, abstract functions cannot be implemented directly, they must be overridden in derived classes of non-abstract classes)

We sometimes do not want to write our own classes for commercial purposes or for other reasons to be called outside of their scope. So we introduced the concept of sealing class and sealing method. We use the keyword sealed to declare sealed classes or sealing methods. For a class, it means that the class cannot be inherited, and for a method, it cannot be overridden.

Before we knew the constructor of a single class the working process now let's look at the constructor of the derived class, first

The constructor's execution order, which is always the constructor of the base class, is called first. (In fact, it is very similar to the recursive invocation process)
( Note : If you explicitly call the constructor of a base class in a subclass, the base class constructor called by the compiler is not a parameter.) That is, if there is no parameterless constructor in the base class, the compiler will error. )

Let's go back and look at the interface, but you'll find that the interface is actually a little bit like the abstract class. The difference is that the interface can only contain member signatures. Interfaces cannot have constructors, cannot have fields, and do not allow the inclusion of operator overloads. The naming convention, when defining the interface name, begins with the capital letter I, so that we know that this is an interface as soon as we see the interface name. In addition we use the interface keyword to define the interface whose syntax is as follows:

[Access modifier] Interface interface Name {//interface body}

The inheritance between interfaces is actually a process of addition, that is, the child interface that is inherited from the parent interface must be greater than or equal to the parent interface. The interface also has a powerful function: interface reference. Why do you say it's strong? Because an interface reference can reference any class that implements the interface. For example: we can construct arrays so that each element of an array is a different class:

ITest is an interface, CTest1, CTest2 is the class that implements the interface itest[] t = new itest[2];t[0] = new CTest1 (); t[1] = new CTest2 ();

In this case, each element of the array becomes a class object of a different class.
( Note : The interface reference cannot refer to a class other than the above, otherwise the compiler will error.) )
Using this feature, we can also use: In a method where CTest1 or CTest2 are required as parameters, the method content is the same, then we just have to:

public void Fun (ITest temp) {//function body}

Then the parameter can be either CTest1 or CTest2. In other words, we don't have to know what kind of reference it is, we just need to know that its object implements the ITest interface is enough.

The above is the inheritance mechanism in C #.

"C # Advanced Programming", "fourth chapter" Inheritance--study notes

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.