ITEEDU

abstract class抽象类

python中没有interface,因为python是可以多继承的,但有abstract class。 python中abstract class也不是原生态的, 还有一个ABC(abstract base class)也可以实现抽象类。

from abc import ABCMeta, abstractmethod

class Drawable():  
   __metaclass__ = ABCMeta

   @abstractmethod
   def draw(self, x, y, scale=1.0):    
       pass

   def draw_doubled(self, x, y):    
       self.draw(x, y, scale=2.0)

class Square(Drawable):
   def draw(self, x, y, scale): 
        pass

c=Square() 

Square类一定要实现draw()方法, 否则, 当实例化一个Square对象时, 将报错TypeError: Can't instantiate abstract class Square with abstract methods draw