Five, Python's object-oriented 9, inheritance and multiple inheritance, and parent class method related to usage

9, inheritance and multiple inheritance, and parent class method and related usage

1) Inheritance

Description :

  • Will be placed in parentheses after the subclass of more than one parent class.

    class SubClass(SuperClass1, SuperClass2, ...):
        # 类定义部分
    
  • From the perspective of subclass, the subclass extends the parent class; from the perspective of the parent, the parent class to subclass.

  • Subclass inherits the parent, the parent class method available.

    class Fruit:
        def taste(self):
            print('水果营养丰富,味道可口')
            
    # Apple 继承了 Fruit
    class Apple(Fruit):
        pass
    
    a = Apple()
    a.taste()    # 继承:子类可以直接复用父类的方法
    
    水果营养丰富,味道可口
    

2) multiple inheritance

class Bird:
    def able(self):
        print('会飞')
        
class Pet:
    def info(self):
        print('漂亮')
        
class Parrot(Bird, Pet):
    pass

b = Parrot()

b.able()
print('-'*30)

b.info()
会飞
------------------------------
漂亮

Analysis : If more than one parent class contains a method with the same name, the top surface of the parent class method will be "covered" with the same name in the row behind the method of the parent class. It is not recommended to use multiple inheritance.

class Bird:    
    def info(self):
        print('羽毛')
        
class Pet:
    def info(self):
        print('漂亮')
        
class Parrot(Bird, Pet):    
    pass

b = Parrot()
b.info()
羽毛

3) override inherited methods

class Bird:
    def fly(self):
        print('会飞')
    
class Ostrich(Bird):
    def fly(self):
        print('鸵鸟说:我不会飞,我只能在地上跑')
        
os = Ostrich()
os.fly()
鸵鸟说:我不会飞,我只能在地上跑

4) After overwriting method call parent class inherited methods

class Bird:
    def fly(self):
        print('会飞')
    
class Ostrich(Bird):
    def fly(self):
        print('鸵鸟说:我不会飞,我只能在地上跑')
        
    def dream(self):
        self.fly()        # 默认情况下,直接调用 fly 方法,总是调用子类重写之后的方法
        print('-'*35)
        
        print('梦见:', end='')
        Bird.fly(self)    # 类名调用方法:未绑定方法,需要显示传入参数        
        
os = Ostrich()
os.dream()
鸵鸟说:我不会飞,我只能在地上跑
-----------------------------------
梦见:会飞

5) calling the parent class constructor

Background :

class Employee:
    def __init__(self, salary):
        self.salary = salary
        
class Manager(Employee):
    def __init__(self, salary, title):
        self.salary = salary              
        self.title = title
        
mg = Manager(7000, '项目经理')
print(mg.title, mg.salary)
项目经理 7000

Analysis :

  • Parent (the Employee) and subclasses (Manager) in both instance variables -salary, repeated redundant.
  • When the salary needs to be changed (such as salary salary * 1.5), requires several changes.

Therefore, the sub-class constructor should call the parent class constructor directly to facilitate post-upgrade project.

  • Method 1 : name of the class constructor calls the parent class (unbound method, you need to pass parameters)

    class Employee:
        def __init__(self, salary):
            self.salary = salary*1.5        # 更改
            
    class Manager(Employee):
        def __init__(self, salary, title):
            Employee.__init__(self, salary)  # 类名调用父类构造方法              
            self.title = title
    
            
    mg = Manager(7000, '项目经理')
    print(mg.title, mg.salary)
    
    项目经理 10500.0
    
  • Second way : The super () function call the parent class constructor (no incoming self parameter)

    class Employee:
        def __init__(self, salary):
            self.salary = salary* 1.5      # 更改
            
    class Manager(Employee):
        def __init__(self, salary, title):
            super().__init__(salary)       #   super() 函数调用父类构造方法             
            self.title = title
    
            
    mg = Manager(7000, '项目经理')
    print(mg.title, mg.salary)
    
    项目经理 10500.0
    

Guess you like

Origin blog.csdn.net/qq_36512295/article/details/94548509