#抽象類實例 做用統一規範接口,下降使用複雜度。
import abc
class Animal(metaclass = abc.ABCMeta): ##只能被繼承,不能實例化,實例化會報錯
@abc.abstractmethod ##加完這個方法子類必須有這個方法,不然報錯
def run(self):
pass
@abc.abstractmethod
def eat(self):
pass
class People(Animal):
def run(self):
print("People is walking")
def eat(self):
print("people is eating")
class Pig(Animal):
def run(self):
print("Pig is running")
def eat(self):
print("people is eating")
class Dog(Animal):
def run(self):
print("Dog is zouing")
def eat(self):
print("dog is eating")
peo1 = People()
pig1 = Pig()
dog1 = Dog()
peo1.run()
peo1.eat()
通用作法。
@six.add_metaclass(MetaClass) 的做用是在不一樣版本的Python之間提供一個優雅的聲明類的metaclass的手段,事實上不用它也能夠,只是使用了它代碼更爲整潔明瞭。
|
import six
@six.add_metaclass(Meta)
class MyClass(object):
pass
|
在Python 3 等價於 |
import six
class MyClass(object, metaclass = Meta):
pass
|
在Python 2.x (x >= 6)中等價於 |
import six
class MyClass(object):
__metaclass__ = Meta
pass
|
或者直接調用裝飾器,
這裏也能看出來裝飾器就是個方法包裝而已。
|
import six
class MyClass(object):
pass
MyClass = six.add_metaclass(Meta)(MyClass)
|
import abc
import six
@six.add_metaclass(abc.ABCMeta)
class BaseClass(object):
@abc.abstractmethod
def func_a(self, data):
"""
an abstract method need to be implemented
"""
@abc.abstractmethod
def func_b(self, data):
"""
another abstract method need to be implemented
"""
class SubclassImpl(BaseClass):
def func_a(self, data):
print("Overriding func_a, " + str(data))
@staticmethod
def func_d(self, data):
print(type(self) + str(data))
class RegisteredImpl(object):
@staticmethod
def func_c(data):
print("Method in third-party class, " + str(data))
BaseClass.register(RegisteredImpl)
if __name__ == '__main__':
for subclass in BaseClass.__subclasses__():
print("subclass of BaseClass: " + subclass.__name__)
print("subclass do not contains RegisteredImpl")
print("-----------------------------------------------")
print("RegisteredImpl is subclass: " + str(issubclass(RegisteredImpl, BaseClass)))
print("RegisteredImpl object is instance: " + str(isinstance(RegisteredImpl(), BaseClass)))
print("SubclassImpl is subclass: " + str(issubclass(SubclassImpl, BaseClass)))
print("-----------------------------------------------")
obj1 = RegisteredImpl()
obj1.func_c("RegisteredImpl new object OK!")
print("-----------------------------------------------")
obj2 = SubclassImpl() #因爲沒有實例化全部的方法,因此這裏會報錯 Can't instantiate abstract class SubclassImpl with abstract methods func_b
obj2.func_a("It's right!")
|
結果以下:
subclass of BaseClass: SubclassImpl
subclass do not contains RegisteredImpl
-----------------------------------------------
RegisteredImpl is subclass: True
RegisteredImpl object is instance: True
SubclassImpl is subclass: True
-----------------------------------------------
Method in third-party class, RegisteredImpl new object OK!
-----------------------------------------------
Traceback (most recent call last):
File "/Users/wangqi/Git/Python/scrapy_crawler_learn/test/ABCTest.py", line 51, in <module>
obj2 = SubclassImpl() #因爲沒有實例化全部的方法,因此這裏會報錯 Can't instantiate abstract class SubclassImpl with abstract methods func_b
TypeError: Can't instantiate abstract class SubclassImpl with abstract methods func_b
|