zoukankan      html  css  js  c++  java
  • 多态

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

  • 相关阅读:
    Excel相关界面
    延时函数
    VBA窗体之ListView分页显示
    VBA窗体之ListView
    医疗机构税收大全及理论探讨(2018年)
    VBA_Xpath定位元素
    VBA_CSS定位元素
    VBA_DOM定位元素
    VBA_IE&HTTP
    Listbox
  • 原文地址:https://www.cnblogs.com/brace2011/p/9291447.html
Copyright © 2011-2022 走看看