抽象类:不能直接创建实例,会报错,一般是为了实现子类公共功能的封装。抽象方法:不能直接调用,只为了实现强调特有方法。
import abc
class Person(object, metaclass=abc.ABCMeta):
@ abc.abstractmethod
def jump(self):
print('跳起来')
def eat(self):
print('XXXXXXXX')
class Man(Person):
def run(self):
print('跑的很快')
class Woman(Person):
def run(self):
print('跑的一般')
# p = Person() # 不能使用抽象类直接创建实例
m = Man()
m.run()
m.eat() # 子类实例可以使用抽象类中的方法
m.jump() # 子类实例不可以使用抽象类中的抽象方法