class animal(object): def run(self): print('animal is running...')
class dog(animal): def run(self): print('dog is running...')
class cat(animal): def run(self): print('cat is running...')
class Tortoise(animal): def run(self): print('tortoise is running slowly...')
class Timer(object): def run(self): print('Start...')
def run_twice(animal): animal.run() animal.run()
run_twice(animal()) run_twice(Tortoise()) run_twice(dog()) run_twice(cat()) run_twice(Timer())
run_twice(Timer()) start...start... 不一定需要传入Animal类型,只需要保证传入的对象有一个run()方法就可以了。是这个意思吧!
额...类名最好大写。比较好区分
Sign in to make a reply
1cfeng
class animal(object): def run(self): print('animal is running...')
class dog(animal): def run(self): print('dog is running...')
class cat(animal): def run(self): print('cat is running...')
class Tortoise(animal): def run(self): print('tortoise is running slowly...')
class Timer(object): def run(self): print('Start...')
def run_twice(animal): animal.run() animal.run()
run_twice(animal()) run_twice(Tortoise()) run_twice(dog()) run_twice(cat()) run_twice(Timer())