C # Object-oriented---what is inheritance?

Source: Internet
Author: User

You may have seen the dog's blood "inheritors", OK, in the process of inheritance, there is the point of ' inheritance ' meaning, but not the same. Give a small Li Zilai said: Your grandfather's money, sooner or later is your father's, your father's, and sooner or later is your. This is inheritance. And what about the succession in the program?

To know how inheritance is going, first figure out why you have to inherit this thing, that is, the purpose. An example might be better understood. For example, you are writing a class < Human, the attribute has name, gender, age. And you need to write another class < student class, the student naturally also has the name, gender and age, but also has his unique attributes (such as grade), do you want to write the common attributes again? Of course it doesn't look like much code, but when you write 10 classes, is there a bright of code redundancy?

So, the (attention to) inheritance came.

Inheritance is something that allows you to write less code , lazy people push progress (vulgar understanding haha). The student class is a subclass of human (derived class ), and Human is the parent class (base class). subclasses inherit the properties and methods of the parent class (do not inherit private fields) and can use these members in subclasses . When creating a subclass object, the object of the parent class is created in the subclass, and the default call to the parent class of the parameterless constructor (something that is easy to initialize).

How to use: Subclass after : (parent class name)

There is also a problem, if the parent class is written with a constructor method, the original default parameterless construction method will be killed, such as the creation of a parent class object in the class will be an error. What do you do?

 1) Re-write an argument-free constructor (low)

2) Use the base keyword to display the constructor of the parent class in the child class

Note that theobject class is the base class for all classes.

What if the method of the subclass is the same as the method name of the parent class? Be warned. Then you can't catch the way of the parent class. If you are deliberately doing this, use the New keyword in the subclass method. such as: Public new void Test ()

Ok?

C # Object-oriented---what is inheritance?

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.