这是一个正常类
python
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
class A:
def f(self):
pass
class B(A):
def g(self):
print("g")
if __name__ == "__main__":
a = A()
b = B()
b.g() # g
b.f()
抽象类(C++中含有纯虚函数的类,)
让A继承ABC,然后给f标上@abstractmethod
,A就是一个抽象类了
必须要实现f才能实例化
python
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
from abc import ABC, abstractmethod
class A(ABC):
@abstractmethod
def f(self):
pass
class B(A):
def f(self):
print("f")
def g(self):
print("g")
class C(A):
pass
if __name__ == "__main__":
# a = A()
b = B()
b.g() # g
b.f() # f
c = C() # TypeError: Can't instantiate abstract class C with abstract methods f