【发布时间】:2020-05-21 06:49:06
【问题描述】:
我有一个简化的类Animal,它可以做food_routine,其中的动作因动物的类型而异:
DOG = 'dog'
CAT = 'cat'
class Animal(object):
def __init__(self, animal_type):
if animal_type not in [CAT, DOG]:
raise Exception('unexpected animal')
self.animal_type = animal_type
def food_routine(self):
self.come_to_plate() # common code
self.sniff_food() # common code
if self.animal_type == CAT:
self.go_around_plate()
self.eat_the_food() # common code
if self.animal_type == DOG: # condition I want to avoid
self.thank_owner()
self.go_away_from_plate() # common code
# cats and dogs do different things after eating:
if self.animal_type == CAT: # condition I want to avoid
self.go_lie_on_couch()
elif self.animal_type == DOG: # condition I want to avoid
self.ask_for_walk_outside()
self.sleep() # common code
def sleep(self):
print 'sleep' # common code
def ask_for_walk_outside(self):
print 'ask for a walk outside'
def go_lie_on_couch(self):
print 'go lie on couch'
def go_away_from_plate(self):
print 'go away from plate' # common code
def thank_owner(self):
print 'thank owner' # only dogs thank owner
def eat_the_food(self):
print 'eat the food' # common code
def go_around_plate(self):
print 'go around plate'
def sniff_food(self):
print 'sniff food' # common code
def come_to_plate(self):
print 'come to plate' # common code
我的问题是 if 语句使代码不可读。我试图通过创建两个类来解决它:一个用于 Dog,一个用于 Cat。每只动物只做他知道在food_routine 中做的事情(并从Animal 中删除了该方法):
class Cat(Animal):
def food_routine(self):
self.come_to_plate() # common code
self.sniff_food() # common code
self.go_around_plate()
self.eat_the_food() # common code
self.go_away_from_plate() # common code
self.go_lie_on_couch()
self.sleep() # common code
class Dog(Animal):
def food_routine(self):
self.come_to_plate() # common code
self.sniff_food() # common code
self.eat_the_food() # common code
self.thank_owner()
self.go_away_from_plate() # common code
self.ask_for_walk_outside()
self.sleep() # common code
这会导致我想要避免的代码重复(common code 行)。
所以我的问题是:什么是避免代码重复和避免if 语句以使代码可读和简单的好习惯?
【问题讨论】:
标签: python oop polymorphism