Python learning path-Inheritance

Source: Internet
Author: User
Tags export class

One of the main benefits of object-oriented programming is code reuse. One of the ways to achieve this reuse is through the inheritance mechanism. Follow
It can be fully understood as the relationship between types and subtypes between classes.
Suppose you want to write a program to record the teachers and students in the school. They have common attributes, such as name, age, and
Address. They also have proprietary attributes, such as teachers' salaries, courses and holidays, students' scores and tuition fees.
You can create two independent classes for the teachers and students to process them. However, if you want to add a new common attribute
This attribute must be added to both independent classes. This will soon become impractical.
A better way is to create a common class called SchoolMember and then let the classes of teachers and students inherit this common
Class. That is, they are all subtypes of this type (class), and then we add proprietary attributes for these subtypes.
This method has many advantages. If we add/change any function in SchoolMember, it will automatically reflect the sub-
Type. For example, if you want to add a new ID field for both teachers and students, simply add it to SchoolMember.
Class. However, the changes made in a subtype do not affect other subtypes. Another advantage is that you can
Elephants are used as SchoolMember objects, which is particularly useful in some occasions, such as counting the number of school members. A sub-type
When the parent type is required, it can be replaced with the parent type, that is, the object can be regarded as an instance of the parent class. This phenomenon is called polymorphism.
In addition, we will find that when reusing the code of the parent class, we do not need to repeat it in different classes. If we use an independent
We have to do this.
In the preceding scenario, the SchoolMember class is called a basic class or a superclass. The Teacher and Student classes are called export classes or
Subclass.
Now we will learn an example program.
[Python]
#! /Usr/bin/python
# Filename: inherit. py
Class SchoolMember:
''''' Represents any school member .'''
Def _ init _ (self, name, age ):
Self. name = name
Self. age = age
Print '(Initialized SchoolMember: % s)' % self. name
Def tell (self ):
''' 'Tell my details .'''
Print 'name: "% s" Age: "% s" '% (self. Name, self. age ),
Class Teacher (SchoolMember ):
''' Represents a teacher .'''
Def _ init _ (self, name, age, salary ):
SchoolMember. _ init _ (self, name, age)
Self. salary = salary
Print '(Initialized Teacher: % s)' % self. name
Def tell (self ):
SchoolMember. tell (self)
Print 'salary: "% d" '% self. Salary
Class Student (SchoolMember ):
''''' Represents a student .'''
Def _ init _ (self, name, age, marks ):
SchoolMember. _ init _ (self, name, age)
Self. marks = marks
Print '(Initialized Student: % s)' % self. name
Def tell (self ):
SchoolMember. tell (self)
Print 'Marks: "% d" '% self. Marks
T = Teacher ('Mrs. shrividya', 40,300 00)
S = Student ('horst', 22, 75)
Print # prints a blank line
Members = [t, s]
For member in members:
Member. tell () # works for both Teachers and Students
Output
[Html]
(Initialized SchoolMember: Mrs. Shrividya)
(Initialized Teacher: Mrs. Shrividya)
(Initialized SchoolMember: Horst)
(Initialized Student: Horst)
 
Name: "Mrs. Shrividya" Age: "40" Salary: "30000"
Name: "Horst" Age: "22" Marks: "75"

To use inheritance, we use the basic class name as a tuples after the class name when defining the class. Then, we noticed that
The _ init _ method of this class is called using the self variable, so that we can initialize the basic class part of the object. 01:10 important-
-Python does not automatically call the constructor of the basic class. You must call it in person.
We also observe that we add the class name prefix before the method call, and then pass the self variable and other parameters to it.
Note that when we use the tell method of the SchoolMember class, we only use the instance of Teacher and Student
SchoolMember instance. Www.2cto.com
In addition, in this example, we call the tell method of the child type, instead of the tell method of the SchoolMember class. Yes.
To understand, Python always first looks for the corresponding type of method, as in this example. If it cannot be found in the export class
To the basic class. The basic class is specified in the tuples when the class is defined.

Annotation of a term-if more than one class is listed in the inheritance tuples, it is called multi-inheritance.

Okay, the entire object-oriented object is here. We have studied multiple Content of classes and objects and multiple terms related to them. Through this chapter, you have learned about object-oriented
Programming advantages and disadvantages. Python is a highly object-oriented language. Understanding these concepts will help you further learn Python in the future.

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.