多继承

2019-02-12  本文已影响0人  匿隱

多继承:

class 类名(父类1, 父类2,....):
      类的内容

多继承的时候,多个父类中的所有方法和字段都可以继承,只是对象属性只能继承第一个父类的

class Animal:
    def __init__(self, name=''):
        self.name = name
        self.age = 0
        self.color = '黑色'

    def fun1(self):
        print('动物中的对象方法')


class Fly:
    def __init__(self):
        self.height = 1000

    def func2(self):
        print('飞行类的对象方法')


class Bird(Animal, Fly):
    pass


def main():
    b1 = Bird()
    b1.fun1()
    b1.func2()
    print(b1.name, b1.age)
    # print(b1.height)  # 'Bird' object has no attribute 'height'
上一篇 下一篇

猜你喜欢

热点阅读