python练习题-day24

2019-10-23 15:04:34 浏览数 (1)

1、单继承

代码语言:javascript复制
class Animal:
    def __init__(self,name,hp,aggr):
        self.name=name
        self.hp=hp
        self.aggr=aggr
    def eat(self):
        print("吃药回血")
        self.hp =50
class Person(Animal):
    def __init__(self,name,hp,aggr,sex,money):
        Animal.__init__(self,name,hp,aggr)
        self.sex=sex
        self.money=money
class Dog(Animal):
    def __init__(self,name,hp,aggr,kind):
        super().__init__(name,hp,aggr)
        self.kind=kind
    def eat(self):
        Animal.eat(self)
        self.teeth=2
alex=Person("alex",100,50,"male",1000)
jin=Dog("金老板",500,100,"teddy")
print(alex.__dict__)
print(jin.__dict__)
jin.eat()
print(jin.__dict__,jin.hp,jin.teeth)

2、多继承

代码语言:javascript复制
class D:
    def fun(self):print("D")
class B(D):pass
    # def fun(self):print("B")
class C(D):
    def fun(self):print("C")
class A(B,C):
    pass
a=A()
a.fun()

print(D.mro())

0 人点赞