Python class inheritance

Source: Internet
Author: User

Python class inheritance
I. Overview

One of the main functions of Object-Oriented Programming (OOP) is "inheritance ". Inheritance refers to the ability to use all the functions of an existing class and extend these functions without re-writing the original class.

The new class created by inheritance is called "subclass" or "derived class", and the inherited class is called "base class", "parent class", or "superclass, it is a process from general to special. In some OOP languages, a subclass can inherit multiple base classes. However, in general, a subclass can only have one base class. To implement multi-inheritance, You can implement multi-level inheritance.

There are two main methods to implement the concept of inheritance: Implementation inheritance and interface inheritance.

When considering inheritance, note that the relationship between the two classes should be "Belong. For example, if the Employee is a Person and the Manager is a Person, both classes can inherit the Person class. However, the Leg class cannot inherit the Person class, because the Leg is not a Person.

The OO development paradigm is roughly: dividing objects → abstract classes → organizing classes into hierarchical structures (Inheritance and synthesis) → designing and implementing classes and instances.

 

Ii. Inheritance of classes 2.1 Definition of Inheritance
Class Person (object): # define a parent class def talk (self): # print ("person is talking .... ") class Chinese (Person): # defines a subclass that inherits the Person class def walk (self): # defines its own method in the subclass print ('is walking... ') c = Chinese () c. talk () # Call the inherited Person class method c. walk () # call its own method # output person is talking .... is walking...

 

2.2 inheritance of Constructors

If we want to pass parameters to instance c, we need to use the constructor. How should the constructor inherit and define its own attributes in the subclass?

Constructor of the inheritance class:

1. Writing a classic class: parent class name. _ init _ (self, parameter 1, parameter 2 ,...)

2. Writing new classes: super (subclass, self). _ init _ (parameter 1, parameter 2 ,....)

Class Person (object): def _ init _ (self, name, age): self. name = name self. age = age self. weight = 'weight' def talk (self): print ("person is talking .... ") class Chinese (Person): def _ init _ (self, name, age, language): # inherit first, and reconstruct Person. _ init _ (self, name, age) # inherits the constructor of the parent class, and can also be written as super (BlackPerson, self ). _ init _ (name, age) self. language = language # define the attributes of a class
Def walk (self): print ('is walking... ') class American (Person): passc = Chinese ('bigberg', 22, 'China ')

If we simply define a constructor in the sub-class Chinese, we are actually restructuring it. In this way, the subclass cannot inherit the attributes of the parent class. Therefore, when defining the constructor of a subclass, we must first inherit and then construct the constructor. In this way, we can also obtain the attributes of the parent class.

The basic process of the sub-class constructor is as follows:

Instantiate Object c ----> c call subclass _ init _ () ----> subclass _ init _ () inherits parent class _ init __() -----> call parent class _ init __()

 

2.3 override of the parent class method by subclass

If we need to modify the method of the base class/parent class, we can reconstruct this method in the subclass. The following talk () method

Class Person (object): def _ init _ (self, name, age): self. name = name self. age = age self. weight = 'weight' def talk (self): print ("person is talking .... ") class Chinese (Person): def _ init _ (self, name, age, language): Person. _ init _ (self, name, age) self. language = language print (self. name, self. age, self. weight, self. language) def talk (self): # subclass reconstruction method print ('% s is speaking chinese' % self. name) def walk (self): print ('is walking... ') c = Chinese ('bigberg', 22, 'China') c. talk () # output bigberg 22 weight Chinesebigberg is speaking chinese

  

Iii. class inheritance links

Class SchoolMember (object): ''' learning member base class ''' member = 0 def _ init _ (self, name, age, sex): self. name = name self. age = age self. sex = sex self. enroll () def enroll (self): 'register 'print ('just enrolled a new school member [% s]. '% self. name) SchoolMember. member + = 1 def tell (self): print ('---- % s ----' % self. name) for k, v in self. _ dict __. items (): print (k, v) print ('---- end -----') def _ del _ (self ): print ('remove [% s] '% self. name) SchoolMember. member-= 1 class Teacher (SchoolMember): 'instructor 'def _ init _ (self, name, age, sex, salary, course): SchoolMember. _ init _ (self, name, age, sex) self. salary = salary self. course = course def teaching (self): print ('Teacher [% s] is teaching [% s] '% (self. name, self. course) class Student (SchoolMember): 'student 'def _ init _ (self, name, age, sex, course, tuition): SchoolMember. _ init _ (self, name, age, sex) self. course = course self. tuition = tuition self. amount = 0 def pay_tuition (self, amount): print ('student [% s] has just paied [% s] '% (self. name, amount) self. amount + = amountt1 = Teacher ('wusir ', 28, 'M', 3000, 'python') t1.tell () s1 = Student ('haitao', 38, 'M ', 'python', 30000) s1.tell () s2 = Student ('lichuang', 12, 'M', 'python', 11000) print (SchoolMember. member) del s2print (SchoolMember. member) # output ---- end ----- just enrolled a new school member [haitao]. ---- haitao ---- age 38sex Mname haitaoamount 0 course pythontuition 30000 ---- end ----- just enrolled a new school member [lichuang]. 3. Remove [lichuang] 2. Remove [Wusir] and remove [haitao].

  

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.