• 多态


    多态:
     一种接口多种形态;
     
     作用,实现接口的重用
     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

  • 相关阅读:
    第六周作业
    第六周上机练习
    第十二周作业
    第十二周上机练习
    第十一周作业
    第十一周上机练习
    第十周上机练习
    第九周上机练习
    第八周作业
    第八周上机练习
  • 原文地址:https://www.cnblogs.com/brace2011/p/9291447.html
Copyright © 2020-2023  润新知