1.特点
- 规定继承类必须具有抽象基类指定的方法(继承抽象基类的类)
- 抽象基类无法实例化
由于这两个特性,抽象基类主要用于接口设计
实现抽象基类需要使用内置的abc模块
import abc
class Human(metaclass=abc.ABCMeta):
"""基类,定义一个抽象类"""
@abc.abstractmethod
def introduce(self):
print('introduce....')
@abc.abstractmethod
def hello(self):
print('hello')
class Person(Human):
#继承于抽象类Human的子类,必须具有抽象基类Human的所有方法
def introduce(self):
print('person...')
def hello(self):
print('person hello')
#抽象类无法实例化
#h=Human()会报错,因为Human是抽象类,无法实例化
p=Person()
p.introduce()
p.hello()
结果
person...
person hello