C # basic parsing VI [inheritance]

Source: Internet
Author: User

Today I will discuss with you the object-oriented Feature 2 in C ---Inheritance

What is inheritance? A simple sentence is: establish the relationship between classes and implementCodeTo facilitate system expansion. To put it bluntly, there are two key points: A. Avoid code redundancy and B.ProgramExtension.

Next we will proceed furtherUnderstanding inheritanceInheritance is one of the main features of object-oriented programming. It can greatly enhance code reusability and save time for programming. Inheritance establishes an intersection between classes so that instances of the newly defined Derived classes can inherit the features and capabilities of the existing base classes, you can also add new features or modify existing features to create a new class level. For example, a subclass (derived class) can inherit some members of the parent class (base class, like the member methods of a class, we can also define the concepts of attributes such as overload, virtual attributes, abstract attributes, and sealed attributes. The two main features inherited are pass-through and single-stick.

In our life, we also implement inheritance, for example:

Let's take a look at a simple example:

Create parent class:

 1   Class Dog
2 {
3 String Type;// Type
4 Public String Type
5 {
6 Get { Return Type ;}
7 Set {Type = value ;}
8 }
9 Int Weight; // Weight
10 Public Int Weight
11 {
12 Get { Return Weight ;}
13 Set {Weight = value ;}
14 }
15 Int Height; // Height
16 Public Int Height
17 {
18 Get { Return Height ;}
19 Set {Height = value ;}
20 }
21 Public Dog (){} // Construction method without Parameters
22 Public Dog ( String T, Int W, Int H) // Construction Method with Parameters
23 {
24 This . Type = T;
25 This . Weight = W;
26 This . Height = h;
27 }
28 }

Add a subclass of dog

 1   Class Collie: Dog //  Subclass 
2 {
3 Public Collie () // Construction method without Parameters
4 {
5 This . Type = " Colec " ;
6 This . Weight = 34 ;
7 This . Height = 66 ;
8 }
9 Public Void Act () // Methods to output the characteristics of Collie dogs
10 {
11 Console. writeline (" Character: Gentle, lively, loyal, and friendly " );
12 }
13 }
14 Class Sheltlandsheepdog: Dog // Subclass
15 {
16 Public Sheltlandsheepdog () // Construction method without Parameters
17 {
18 This . Type = " Hilti " ;
19 This . Weight = 18 ;
20 This . Height = 30 ;
21 }
22 Public Void Act () // Methods output the features of Hilti
23 {
24 Console. writeline ( " Character: Gentle, loyal, and smart dog breed " );
25 }
26 }

Instantiate subclass objects in the main method and call their methods:

 1   Static   Void Main ( String [] ARGs)
2 {
3 Console. writeline ( " Features " );
4 Collie = New Collie ();
5 Collie. Act (); // Call Method
6 Console. writeline ( " Features of Hilti " );
7 Sheltlandsheepdog = New Sheltlandsheepdog ();
8 Sheltlandsheepdog. Act (); // Call Method
9 Console. readkey ();
10 }

Running result:

Base keyword

In inheritance, the base keyword is used to inherit the parent class. When we use this method, we usually use base () to override, overwrite, and call this method in the parent class (), this base seems to be similar to the super in Java. net, not very familiar with Java ). C # it's similar to base. Another one is this! In fact, these two are also very similar in use, but there is still a difference, this is to itself, base is to parent class. For example, if you have int N in the parent class and you have int N in the subclass, then base. N and this. N indicates not the same variable, but if n is not defined in the subclass, then base. N and this. n is the same variable.

Combined with the above example, the use of base in the Construction Method with Parameters

 
PublicQingpingguo (StringT,IntW,IntH ):Base(T, W, h ){}

Let's take a look.Inheritance passed
A derived class inherits features from the base class, so the derived class can also be used as the base class of other classes. A multi-layer class is derived from a base class, which forms a class hierarchy. Just like cars, and then cars are divided into trucks and buses, then trucks and buses are derived classes, and trucks can be derived from small trucks and heavy trucks as the base class. In short, child classes not only inherit the members of the parent class, but also inherit the members of the parent class. Let's look at a picture and we will understand:

Inheritance
The root inheritance feature means that a subclass can inherit only one parent class and cannot inherit multiple parent classes at the same time. Just like a son can only have one father (his own father ),A derived class can only inherit from one class. Inheritance does not support multiple inheritance. This rule is designed to avoid the complexity of the code structure.

Finally, we will introduce two attributes:

One is abstract attribute
Attributes declared using the abstract modifier are abstract attributes.
The accessors of abstract attributes are also virtual, and the specific implementations of accessors are not provided. This requires that, in a non-virtual derived class, the derived class itself provides the specific implementation of the accesser by reloading the attribute.
Abstract and override modifiers are used at the same time, which not only indicates that the attributes are abstract, but also overload the virtual attributes in the base class. In this case, the attribute accessors are also abstract.
Abstract attributes can only be declared in abstract classes.
In addition to the use of abstract and override modifiers, either of static, virtual, override, or abstract modifiers cannot appear at the same time.
Sealing properties
The attribute declared using the sealed modifier is a seal attribute. The class sealing attribute cannot be inherited in the derived class. The accessors of the sealed attribute are also sealed.
If a sealed modifier exists during attribute declaration, the override modifier must also exist.

Summary:

1. Understand inheritance;

2. Base keyword;

3. Inheritance transmission;

4. Single inheritance;

 

Okay! This is today! I can't open my eyes! I feel that writing is a little hasty, because there are a lot of things not written! However, I still hope to help beginners! I think this evening is not a waste of time for beginners!

This article is my personal opinion. If there are any imperfections or inaccuracies, you are welcome to criticize them.

Author: green apple
Motto: constantly reflect on yourself! Then change it!
Technologies of interest:. net, database, JavaScript, C #, Ajax, winform, jquery, extjs
Source: http://www.cnblogs.com/xinchun/
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.
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.