本文章主要总结利用abc模块实现的python抽象类和抽象方法的编写。
步骤:
1 导入abc模块
import abc
2 编写抽象类
- class class1(object):
- __metaclass__ = abc.ABCMeta
- #抽象方法编写
- @abc.abstractmethod
- def method1(self):
- return
- #抽象属性编写
- def getx(self):
- return
- def setx(self, value):
- return
- x = abc.abstractproperty(getx, setx)
3 编写子类
-继承class1
- class SubClass(class1):
- #重写method1
- def method1(self):
- print('method1')
- return
- #重写属性方法
- def getx(self):
- return self.x
- def setx(self, value):
- self.x=value
- x=property(getx, setx)