二十二. Python基础(22)--继承

时间:2023-12-18 10:23:38

二十二. Python基础(22)--继承

● 知识框架

二十二. Python基础(22)--继承

二十二. Python基础(22)--继承

 

● 继承关系中self的指向

当一个对象调用一个方法时,这个方法的self形参会指向这个对象

class A:

    def get(self):

        self.say()

 

    def say(self):

        print('AAAAA')

 

class B(A):

    def say(self):

        print('BBBBB')

 

b = B()

b.get() # BBBBB

# 当一个对象调用一个方法时,这个方法中的self就指向这个对象

# 子类先调用自己的属性或方法, 子类自己没有才调父类的

# 如果是class B(A):pass, 那么结果是'AAAAA'

print(dir(b))

# ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'cls_att', 'get', 'name', 'say']

print(b.__dict__)

# {'name': 'Arroz'}

print(vars(b))

# {'name': 'Arroz'}

print(b.__class__)

# <class '__main__.B'>

print(B.__class__)

#<class 'type'> # 实例关系

print(B.__bases__)

# (<class '__main__.A'>,) # 继承关系

print(B.__doc__)

# This is class B

 

● 经典类(old-style)和新式(new-style)类调用父类方法的区别

class Dog(Animal):

    def __init__(self, name):

        # Animal.__init__(self, name) # 经典类, 注意不要遗漏self

        super().__init__(name) # 新式类, 等价于super(Dog, self).__init__(name),读作"调用Dog的父类

         的方法(calling Dog's parent's method)"

内置函数super([type[, object-or-type]])

Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used by getattr() except that the type itself is skipped.

 

● 综合案例

class Base:

    cls_attr = 'class attribute'

    def __init__(self, name, age, job):

        self.name = name

        self.age = age

        self.job = job

    def bmeth(self):

        print('Base method')

 

class Derived_0(Base):

    def dmeth(self):

        print('Derived method')

 

print(type(Base)) # <class 'type'>, 元类

print(type(Derived_0)) # <class 'type'>, 元类

 

# 没有添加新的对象属性, 不用使用super()

d_0 = Derived_0('Paul', 55, 'Singer')

print(d_0.job)

 

# 下面添加了添加了新的对象属性, 需要使用super()

class Derived_1(Base):

    def __init__(self, name, age, job, hobby):

        super(Derived_1, self).__init__(name, age,job) #
子类调用父类方法时, 参数的个数不能缺少.

        self.hobby = hobby

    def dmeth(self):

        print('Derived method')

    def show(self):

        print(self.name, self.age, self.job,self.hobby)

 

class Derived_2(Base):

    def __init__(self, name, hobby):

        super(Derived_2, self).__init__(name, 20, 'teacher') # 子类调用父类方法时, 参数的个数不能缺少, 并且可以把已知的属性先传给父类的构造函数

        self.hobby = hobby

    def dmeth(self):

        print('Derived method')

    def show(self):

        print(self.name, self.age, self.job,self.hobby)

 

class Derived_3(Base):

    def __init__(self, hobby):

        super(Derived_3, self).__init__("Paul", 22, 'painter') # 子类调用父类方法时, 参数的个数不能缺少, 并且可以把已知的属性先传给父类的构造函数

        self.hobby = hobby

 

    def dmeth(self):

        print('Derived method')

 

    def show(self):

        print(self.name, self.age, self.job, self.hobby)

 

#① #######################################################

b = Base('Arroz', 18, 'teacher')

print(Base.__dict__) # 类的属性

'''

{'__module__': '__main__', 'cls_attr': 'class attribute', '__init__': <function Base.__init__ at 0x00000000028EA840>, 'bmeth': <function Base.bmeth at 0x00000000028EA8C8>,

'__dict__': <attribute '__dict__' of 'Base' objects>, '__weakref__': <attribute '__weakref__' of 'Base' objects>, '__doc__': None}

'''

print(b.__dict__) # 对象属性

'''

{'name': 'Arroz', 'age': 18, 'job': 'teacher'}

'''

print('----------------------')

#② #######################################################

d_1 = Derived_1('Arroz', 18, 'teacher','swimming')

d_1.show() # Arroz 18 teacher swimming

print('----------------------')

#③ #######################################################

d_2 = Derived_2('Pwter', 'skating')

d_2.show() # Pwter 20 teacher skating

print('----------------------')

#④ #######################################################

d_3 = Derived_3('jogging')

d_3.show() # Paul 22 painter jogging