Python3 继承

继承的好处:子类实现父类的全部功能

1、单继承

  • 若父类和子类有共同的方法或属性,则子类对父类方法或属性进行覆盖
    class ClassA:
        def __init__(self):
            self.a = 'a'
            print('init A')
    
        def sayName(self):
            print('Name')
    
    class ClassB(ClassA):
        def __init__(self):
            self.b = 'b'
            print('init B')
    
    
    b = ClassB() # init B
    b.sayName()  # Name
    print(b.a)   # AttributeError: 'ClassB' object has no attribute 'a'

猜你喜欢

转载自www.cnblogs.com/AndyChen2015/p/11428153.html