C # inheritance

Source: Internet
Author: User

Inheritance in C # is divided into implementation inheritance and interface inheritance. Today we will focus on implementation inheritance.

Inheritance indicates that one type is derived from another base type, which has all member fields and functions of the base type. In implementation inheritance, each function of the derived type adopts the implementation of the base type.Code. -- From page 92 of C # advanced programming version 6

Why do we need to use class inheritance? For example, if we have a class called peoplo (human) which has attributes such as height and weight, at the same time, it also has methods such as eating and sleeping, but because of the advancement of science and technology, people have invented the robot, it can not only have height, weight, but also can eat, sleep, it also has functions that humans do not possess. For example, a robot can dive into a deep water of less than 1000. If it does not inherit, when defining the robot class, we need to repeatedly define peoplo members and unique members of the robot, which increases our burden and can solve this problem well by using inheritance, as shown below:

Peoplo class first
1 public class peoplo
2 {
3 Public int stature {Get; set;} // height
4 Public int avoirdupois {Get; set;} // weight
5 Public peoplo ()
6 {
7 //
8 // todo: add the constructor logic here
9 //
10}
11 Public String dining () // meal
12 {
13 return "Today's braised pork is delicious! ";
14}
15 Public String dreamland () // sleep
16 {
17 return "Why isn't Simmons's mattress? ";
18}
19}

This class defines the stature and avoirdupois attributes, dining and dreanland methods.
Let's take a look at the robot class.
 

 

1 public class ROBOT: peoplo
2 {
3 Public robot ()
4 {
5 //
6 // todo: add the constructor logic here
7 //
8}
9 Public String dive ()
10 {
11 return "has been down to 999 meters, and will soon reach 1000 Miller! ";
12}
13}
I think it is necessary to first figure out what is a base class and robot is derived from peoplo, then, robot is the derived class, And peoplo is the base class. You can also say that robot is the derived class of peoplo, and peoplo is the base class of robot.

Next, we use robot RB = new robot (). In this way, we get a robot type object RB. So what are the members of RB? That's right, because robot inherits the peoplo class. Therefore, it has two attributes: stature and avoirdupois, dining and dreanland, and its own dive () method. We create a default. aspx page to verify the output.
1 public partial class _ 4_default: system. Web. UI. Page
2 {
3 robot RB = new robot ();
4
5 protected void page_load (Object sender, eventargs E)
6 {
7 response. Write (RB. Dining ());
8}
9}

Running result: the braised pork is delicious today!

Oh, how have robots started to eat meat? technology advances and robots cannot eat meat. It seems that attributes and methods in the peoplo class are not necessarily applicable to robot, humans use meat to supplement energy. What does a robot use to supplement energy? Yes, electricity! It seems that it is necessary for us to redefine the eating action in the robot class. Some people may say that we can define a rotbodining () method, but it is not necessary! We only need to modify the robot and peoplo classes, as shown below:
1 public class peoplo
2 {
3 Public int stature {Get; set;} // height
4 Public int avoirdupois {Get; set;} // weight
5 Public peoplo ()
6 {
7 //
8 // todo: add the constructor logic here
9 //
10}
11 Public Virtual string dining () // dinner
12 {
13 return "Today's braised pork is delicious! ";
14}
15 Public String dreamland () // sleep
16 {
17 return "Why isn't Simmons's mattress? ";
18}
19}

After transformation, peoplo does not significantly change from the original one. It is just that dining () has a virtual keyword. What is its role? You will know later. Let's take a look at the transformed robot class, as shown below.
 

 

 
1 public class ROBOT: peoplo
2 {
3 Public robot ()
4 {
5 //
6 // todo: add the constructor logic here
7 //
8}
9 Public String dive ()
10 {
11 return "it's already 999 meters, and we're about to reach 1000 Miller! ";
12}
13 public override string dining ()
14 {
15 return "don't let me work, I'm charging! ";
16}
17}
We found that the transformed robot class has a public override string dining () method, which is exactly the same as the signature of the dinind () method in the base class peoplo, and there is also a keyword override, run default first. view the running result on the ASPX page. The result is as follows:

Don't let me work. I'm charging!

Now, how does the dining () of robot meet the actual charging performance of the robot. How is it implemented?

Let's take a look at the public override string dining () of robot. This method is exactly the same as the signature of the dinind () method in the base class peoplo, and there is also a keyword override, the purpose of this declarative method is to override the dining () method in the base class. The key to this declaration is the keyword override, which indicates that I want to override a method in the base class, so which method of the base class should be rewritten? At this time, the method name dining () will come in handy. It indicates which method of the base class should be rewritten, note that the method name must be exactly the same as the method signature in the base class (not only the method name is the same, the parameter type, quantity, and order must be consistent ), if the method signature is inconsistent with the method signature rewritten in the base class, an error occurs.

Is it not necessary to rewrite all methods in the base class in the derived class! This also checks whether the methods in the base class can be overwritten. How can we identify or set the methods in the base class to be overwritten? Let's look back at the transformed peoplo class, as we mentioned above, after transformation, the dining () has a virtual keyword, which defines this method as a virtual method, so that the derived class can override this method, simply put, a method in a base class can override this method only when the virtual method is declared as a virtual method by using the keyword.

So when the derived class overrides the virtual method in the base class, will this method execute the method that is overwritten in the derived class when we call the base class method? In this experiment, we instantiate a robot object Rb and peoplo object PL in default. aspx. CS respectively, and then call the dining () method of the two objects respectively. The Code is as follows:
1 public partial class _ 4_default: system. Web. UI. Page
2 {
3 robot RB = new robot ();
4 peoplo PL = new peoplo ();
5 protected void page_load (Object sender, eventargs E)
6 {
7 response. Write ("the dining () method in Robot:" + RB. Dining () + "<br/> ");
8 response. Write ("dining () method in peoplo:" + pl. Dining ());
9}
10}

Running result:

The dining () method in Robot: Don't let me work. I'm charging!
In peoplo, the dining () method is delicious.
The test results show that although the dining () method in the base class peoplo is rewritten in the robot class of the derived class, this does not affect the method in the base class, that is to say, the method that is rewritten in a derived class only plays a role in the derived class. It cannot change the method that is overwritten in the base class.

Let's talk about the inheritance of classes first. To be honest, it's very simple, but there is also a very important and difficult-to-understand constructor for implementing inheritance. We didn't talk about it, in the next article, I will briefly discuss my personal understanding about this.

From the red black guest Union (www.7747.net) Original: http://www.7747.net/kf/201007/52319.html

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.