面向对象高级特性之--抽象基类

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
发布了36 篇原创文章 · 获赞 0 · 访问量 285

猜你喜欢

转载自blog.csdn.net/ANingL/article/details/103932939
今日推荐