• 多态


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

  • 相关阅读:
    详细对比9门主流编程语言
    ZT在谷歌上班感受如何?
    林锐:5 C++/C程序的基本概念
    林锐书:写一个hello world by seasoned professional
    C 中重载一词中的“重”字读ZHONG4还是CHONG2?
    ZT C++ 重载、覆盖和隐藏的区别
    安全模式 冷启动
    Wi-Fi
    再谈男性饮食保健
    fstat、stat和lstat 区别(转)
  • 原文地址:https://www.cnblogs.com/brace2011/p/9291447.html
Copyright © 2020-2023  润新知