Python多重继承练习

'''
class Animal(object):
def eat(self):
print("吃")

def sleep(self):
print("睡着了...")


class Fly(object):
def fly(self):
print("飞")


class Dog(Animal):
# 覆盖父类方法
def sleep(self):
# 引用父类方法
super().sleep()
print("仰天大睡")

def bark(self):
print("狂吠")


class Bat(Animal, Fly):
def __init__(self, name, age):
self.name = name
self.age = age


class LittleBird(Animal, Fly):
pass


b = Bat("bbb",22)
#b.fly() # 飞

#print(type("hello")) # str
#print(type([1, 2, 3])) # list
#print(type(b)) #__main__.Bat

#print(isinstance(b, Bat)) # True(判断b是否为Bat的实例)
#print(isinstance(b, LittleBird)) # Fause
#print(isinstance(b, Animal)) # True
#print(isinstance(b, Fly)) # True
#print(type(b) == Bat) # True

#print(dir(b)) # (查看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__', 'age', 'eat', 'fly', 'name', 'sleep']
#print(b.__dir__()) #等同于print(dir(b))
print(b.__dict__) #查看b的实例属性(自己代码自定义的){'name': 'bbb', 'age': 22}
'''
posted @ 2022-04-15 23:25  呼长喜  阅读(73)  评论(0编辑  收藏  举报