object-oriented programming (OOP) One of the main functions of language is “ Inherit ”. Inheritance is the ability to : It can use all the functionality of an existing class , And extend these functions without having to rewrite the original class .
A new class created by inheritance is called “ Subclass ” or “ Derived class ”, The inherited class is called “ Base class ”、“ Parent class ” or “ Superclass ”, The process of inheritance , It's a process from general to special . In some OOP In language , A subclass can inherit multiple base classes . But in general , A subclass can only have one base class , To implement multiple inheritance , It can be realized through multi-level inheritance .
The main ways to realize the concept of inheritance are 2 class : Implementation inheritance 、 Interface inheritance .
When considering using inheritance , There is one caveat , That is, the relationship between the two classes should be “ Belong to ” Relationship . for example ,Employee It's a person ,Manager It's also a person , So both classes can inherit Person class . however Leg Class can't inherit Person class , Because legs are not alone .
The development paradigm is roughly : Divide the object → abstract class → Organize classes into hierarchies ( Inheritance and composition ) → Design and implement several stages with classes and instances .
1 The definition of inheritance
class Person(object): # Define a parent class
def talk(self): # Methods in the parent class
print("person is talking....")
class Chinese(Person): # Define a subclass , Inherit Person class
def walk(self): # Define its own methods in subclasses
print('is walking...')
c = Chinese()
c.talk() # Call inherited Person Class method
c.walk() # Call its own method
# Output
person is talking....
is walking...
2 Inheritance of constructors
If we want to give examples c The ginseng , We're going to use constructors , So how do constructors inherit , At the same time, how to define their own properties in subclasses ?
The construction method of inheritance class :
The writing of classic class : Parent class name .__init__(self, Parameters 1, Parameters 2,...)
The new type of writing :super( Subclass ,self).__init__( Parameters 1, Parameters 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 , In refactoring
Person.__init__(self, name=name, age=age) # Inherit the construction method of the parent class , Or you could write it as :super(Chinese,self).__init__(name=name, age=age)
self.language = language # Define the properties of the class itself
def walk(self):
print('is walking...')
class American(Person):
pass
c = Chinese('bigberg', 22, 'Chinese')
If we're just in subclasses Chinese Define a constructor in , In fact, it's refactoring . In this way, the subclass cannot inherit the properties of the parent class . So when we define the constructor of a subclass , You have to inherit before you construct , In this way, we can also get the properties of the parent class .
The process of the subclass constructor is as follows :
Instantiate objects c ----> c Call subclass __init__() ---- > Subclass __init__() Inherited parent class __init__() ----- > Call the parent class __init__()
3 Subclass's rewriting of parent methods
If we're talking about base classes / The method of the parent class needs to be modified , You can refactor this method in subclasses .
As follows 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, nname=name, age=age)
self.language = language
print(self.name, self.age, self.weight, self.language)
def talk(self): # Subclass refactoring
print('%s is speaking chinese' % self.name)
def walk(self):
print('is walking...')
c = Chinese('bigberg', 22, 'Chinese')
c.talk()
# Output
bigberg 22 weight Chinese
bigberg is speaking chinese
''' No one answers the problems encountered in learning ? Xiaobian created a Python Exchange of learning QQ Group :153708845 Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book ! '''
class SchoolMember(object):
''' Learn member base classes '''
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(' Fired [%s]' % self.name)
SchoolMember.member -= 1
class Teacher(SchoolMember):
' Teachers' '
def __init__(self, name, age, sex, salary, course):
SchoolMember.__init__(self, name=name, age=age, sex=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=name, age=age, sex=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 += amount
t1 = 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 s2
print(SchoolMember.member)
Output
----end-----
just enrolled a new school member [haitao].
----haitao----
age 38
sex M
name haitao
amount 0
course python
tuition 30000
----end-----
just enrolled a new school member [lichuang].
3
Fired [lichuang]
2
Fired [Wusir]
Fired [haitao]