Usage scenarios for Python_ class combinations and class inheritance

Source: Internet
Author: User
1. When do I use a combination of classes?
When there are significant differences between classes, and the smaller classes are the components needed to make up a larger class, the combination of classes is more reasonable.
Scene: The hospital is composed of a number of departments, at this time we can define different sections of the class, so that the hospital class we can directly use the different sections of the class to be combined;
2. When do I use the inheritance of classes?
When there are very similar functions between classes, we can extract these common functions into a base class, other classes call the base class directly, and the inheritance of the class is reasonable.
Scenario: Hospitals have patients, doctors, all belong to people, have meals, sleep, age, gender and other methods or characteristics, so that we can first define the human class (including the above methods and characteristics), then we define the patient class and the Doctor class can be directly heir to this class.
Or it can be understood that we define a basic human class, the class of the patient and the doctor, and then derive their own characteristics and methods on the basis of the human class. This approach can also be called a derivation of a class.

Usage scenarios for Python_ class combinations and class 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.