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

  • 相关阅读:
    在android模拟器运行arm 移植的 c程序
    Android实现对c++方式调用
    Hdu 1003 Max Sum
    HDU 1005 Number Sequence
    poj 1222 EXTENDED LIGHTS OUT 高斯消元法
    分治算法
    HDU杭电acm题目分类大全
    HDU 1002 A + B Problem II 大数相加
    HDU 1004 Let the Balloon Rise
    phpcms栏目和专题的区分
  • 原文地址:https://www.cnblogs.com/brace2011/p/9291447.html
Copyright © 2011-2022 走看看