One: the basis of inheritance
[1] based on the concept
(1) inheritance
(1) Definition: a new class definition way
(2) the derived class is called a subclass / derived class is called the parent class is the successor / base class
For example: Sicong Wang Jianlin inherit property so Sicong belong to a subclass and Wang Jianlin belong to the parent class
PS: inheritance relationship between class and class belong in the program
(2) the role of:
(1) subclasses inherit certain properties of the parent class, method, etc.
(2) reuse code subclasses of the parent class code amount increase
(3) use:
(1) syntax: class + class name (parent name):
E.g:
class Father: pass class Son(Father): pass print(Son.__bases__) # (<class '__main__.Father'>,)
[2] inherited extensions:
(1) abstract:
(1) describes a succession What are the relations must inherit the abstract before
(2) is a plurality of abstract subclasses having the same features and attributes extracted to form a new class
(3) inherited an already existing class can modify or extend the existing functionality
Case:
class Father: def __init__(self,name,age): self.name = name self.age = age class Son(Father): def __init__(self, name, age): self.name = name self.age = age fat = Father('爸爸',40) son = Son('儿子',18) print(fat.name) # 爸爸 print(son.name) # 儿子 # PS: There can be seen a lot of duplicate code
PS: There a lot of duplicate code in the code relatively poor organizational structure
Solution:
class the Person: DEF the __init__ (Self, name, Age): the self.name = name self.age = Age class Father (the Person): Print ( ' call from the parent class ' ) class Son (the Person): Print ( ' from the call the parent class ' ) FAT = father ( ' father ' , 40 ) son = son ( ' son ' , 18 is ) print (fat.name) # Dad print(son.name) # Son
PS: a new class of its remaining class inherits this new class reduce redundant code by the same features extracted aggregated into large
(2) Find the order of attributes:
(1) First, find the object itself
Class (2) to find the object belongs
(3) Find the parent class
(4) Find object
E.g:
class the Person: # Class annotated Test = ' parent ' class Father (the Person): # object is annotated Test = ' class ' Pass FAT = Father () fat.test = ' objects ' Print (fat.test) '' ' 1: Object 2: class 3: the parent class ' ''
(3) derived:
(1) When a subclass appear in the parent class when different content
(2) usually sub-class will appear on the parent if different content is identical not make sense
(3) which is primarily to expand its functionality on top of the parent class inherits attributes and methods
E.g:
class Animal: DEF RUN (Self): Print ( ' animals like to run ' ) class Cat (Animal): # extended features new functions DEF EAT (Self): Print ( ' cats like fish ' ) CAT = Cat ( ) # behavior inherits the parent class cat.run () # animals like to run # call the new property cat.eat () # cat likes to eat fish # add your own new property cat.color = ' black sergeant ' Print ( cat.color) # cat likes to eat fish
(4) covering
(1) also referred to overwrite
(2) sub-class emerged with the parent class the same thing
(3) When the object is to subclass prevail at the time of the call
E.g:
class Animal: DEF RUN (Self): Print ( ' animals like to run ' ) class Cat (Animal): DEF RUN (Self): Print ( ' Animal Cat likes to run ' ) # extension of new function- DEF EAT ( Self): Print ( ' cats like fish ' ) cAT = cat () # inherit the parent class behavior cat.run () # animals cat likes to run directly find the class attribute
(4) sub-class is called the parent class:
(1) Inheritance: subclasses that is more similar to the parent class is associated with the time
(2) super: subclasses associated with the parent class is not so close
(3) the name of the parent class name names. Subclass the parent class method associated with the parent class is not so close
Case:
(1)super:
class Phone: DEF Call (Self, name): Print ( ' % S cell phone use ' % name) class the Person (Phone): DEF RUN (Self): . Super () Call ( ' the SR ' ) per = the Person ( ) per.run () # SR using cell phone
(2) nomination Road last name:
class Phone: DEF Call (Self, name): Print ( ' % S cell phone use ' % name) class the Person (Phone): DEF RUN (Self): Phone.call (Self, ' the SR ' ) per = the Person ( ) per.run () # SR using cell phone
PS: its nothing to do with inheritance
II: combination
[1]: basic concepts
(1) concept
(1) What is a relationship describing what relationship
(2) so that as properties of an object in an object in the python
(2) Objective combination:
(1) code reuse is
(2) reduction to reduce the impact of coupling between the code
class Phone: DEF the __init__ (Self, kind,. price,): self.kind = kind self.price = . price DEF Call (Self): Print ( ' is busy ' ) class Student: DEF the __init__ (Self, name, Phone) : the self.name = name self.phone = phone Print ( ' % S is talking on the phone ' % the self.name) Pho = phone ( ' the Apple ' , 9999) STU = Student ( ' the SR ' , Pho) stu.phone.call () # is busy
PS:
(1) Student would like to Phone calls need to be stored in their own namespace
(2) when it is stored in its own name space when the need to maintain a consistent namespace object so the object need to pass parameters
(3) create an object that is instantiated variable a process may further pho class corresponding to an internal function of the parameters passed to the form of the class
(4) receiving the function parameter data and interact
(3) diamond inheritance
(1) new class:
(1) which shows part of an implicit or subclass of object
(2) are all in the new class python3
(2) Classic
(1) does not belong to a subclass of object
(3) which appears only in the python2
(4) diamond search order
(1) Classic:
When you are looking for properties that do not exist will execute multiple inheritance depth look at the situation in python2
Graphic:
(3) new categories:
(1) In python3 if there is no common parent class that performs a depth-first
(2) if it has a common parent class that will perform a breadth-first
PS:
(1) search order can be viewed by mro
(2) this parameter is not in the python2