多态:
 一种接口多种形态;
 
 作用,实现接口的重用
 class Animal(object):
  def __init__(self, name):  # Constructor of the class
   self.name = name
 
  def talk(self):              # Abstract method, defined by convention only
   raise NotImplementedError("Subclass must implement abstract method")
 
 
 class Cat(Animal):
  def talk(self):
   print('%s: 喵喵喵!' %self.name)
 
 
 class Dog(Animal):
  def talk(self):
   print('%s: 汪!汪!汪!' %self.name)
 
 
 
 def func(obj): #一个接口,多种形态
  obj.talk()
 
 c1 = Cat('小晴')
 d1 = Dog('李磊')
 
 func(c1)
 func(d1) 

 

 

 

 真实案例: 
  
 class Flight(object):
  def __init__(self,name):
    self.flight_name = name
 
def checking_status(self):
  print("checking flight %s status " %self.flight_name)
  return 2
 
  @property
  def flight_status(self):
     status = self.checking_status()
     if status == 0:
        print("flight got canceled....")
     elif status == 1:
        print("flight is arrivied....")
     elif status == 2:
        print("flight has departure....")
     else:
        print("can't confirm the flight's status.....")

f = Flight("CA980")
f.flight_status

posted on 2018-07-10 21:04  仙寓游子  阅读(93)  评论(0编辑  收藏  举报