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 to realize code reusability and facilitate system expansion. To put it bluntly, there are two key points: a. Avoiding code redundancy and B. Program expansion.

Next, let's take a closer look at inheritance. inheritance is one of the main features of object-oriented programming. It can greatly enhance code reusability and save programming time. 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 () {}// No parameter Constructor
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 = "Collie Dog ";
6 this. Weight = 34;
7 this. Height = 66;
8}
9 public void Act () // method output the characteristics of a collie dog
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 () // output the characteristics of Hilti
23 {
24 Console. WriteLine ("character: Gentle, loyal, smart dog ");
25}
26}

Instantiate subclass objects in the Main method and call their methods:
1 static void Main (string [] args)
2 {
3 Console. WriteLine ("Characteristics of colpoint ");
4 Collie collie = new Collie ();
5 collie. Act (); // call method www.2cto.com
6 Console. WriteLine ("Hilti's features ");
7 SheltlandSheepdog sheltlandsheepdog = new SheltlandSheepdog ();
8 sheltlandsheepdog. Act (); // CALL THE 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
Public qingpingguo (string t, int w, int h): base (t, w, h ){}
Let's continue to take a look at the transmission of inheritance.
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), and 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.


From apple

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.