python类接口和抽象超类分析

电子说

1.2w人已加入

描述

1.1 导入模块执行class语句

python导入模块时会执行class语句及主体内的顶层语句。

示例

# test.py
class MyClass:
   print('MyClass')
# cmd执行下面语句
E:\\documents\\F盘>python
>>> import test
MyClass

1.2 pytho类接口技术

python类接口技术通过继承实现。

# test.py
class Super:
   '''定义一个超类。
  定义一个method方法。
  定义一个delegate方法,方法内调用子类定义的action方法。
  '''
   def method(self):
       print('in Super.method')
   def delegate(self):
       '''调用子类定义的action'''
       self.action()
   
class Inheritor(Super):pass

class Replacer(Super):
   '''定义 Replacer 继承 Super
  替换超类的 method 方法
  '''
   def method(self):
       print('in Replacer.method')

class Extender(Super):
   '''定义 Extender 继承 Super
  调用超类的 method 方法, 并且进行扩展
  '''
   def method(self):
       print('Begin Extender.method')
       Super.method(self)
       print('End Extender.method')
       
class Provider(Super):
   '''定义 Provider 继承 Super
  实现超类 delegate 内调用的 action 方法
  '''
   def action(self):
       print('in Provider.action')
       
if __name__ == '__main__':
   for cls in (Inheritor,Replacer,Extender,Provider):
       print('\\n{}....'.format(cls.__name__))
       cls().method()
       
   print('\\nProvider')
   p = Provider()
   p.delegate()

''' 运行结果

E:\\documents\\F盘>python test.py

Inheritor....
in Super.method

Replacer....
in Replacer.method

Extender....
Begin Extender.method
in Super.method
End Extender.method

Provider....
in Super.method

Provider
in Provider.action
'''

1.3 python抽象超类

python类的部分行为由子类提供,则为抽象超类。

1.3.1 调用方法时触发提示

显式要求子类必须实现抽象超类的方法:

(1) 方法一,在超类方法用assert False

(2) 方法二,在超类方法用 raise NotImplementedError

未实现,则在实例调用方法时触发报错提示。

子类和超类都未提供方法 ,报 has no attribute

>>> class AbsSuper:
   def delegate(self):
       self.action()
       
>>> class Provider(AbsSuper):pass
>>> p=Provider()
>>> p.delegate()
Traceback (most recent call last):
 File ", line 1, in

在超类方法用assert False

>>> class AbsSuper:
   def delegate(self):
       self.action()
   def action(self):
       assert False,'子类必须定义 action'

       
>>> class Provider(AbsSuper):pass

>>> Provider().delegate()
Traceback (most recent call last):
 File ", line 1, in

在超类方法用raise NotImplementedError

>>> class AbsSuper:
   def delegate(self):
       self.action()
   def action(self):
       raise NotImplementedError('子类必须定义 action')
>>> class Provider(AbsSuper):pass

>>> Provider().delegate()
Traceback (most recent call last):
 File ", line 1, in

1.3.2 创建实例时触发提示

(1) 带有@abstractmethod修饰的方法为抽象方法。

(2) 带有抽象方法的类不能进行实例化。

(3) 超类有抽象方法时,子类必须重写超类的抽象方法。

(4) 未重写,则创建实例时触发报错提示。

抽象方法定义:

(1) python3:超类头部括号送metaclass**=**ABCMeta。

(2) python2:超类主体定义**metaclass****=**ABCMeta。

(3) 用@abstractmethod修饰方法。

python3示例

>>> from abc import ABCMeta,abstractmethod
>>> class MySuper(metaclass=ABCMeta):
   def delegate(self):
       self.action()
   # @abstractmethod 为抽象方法,子类必须重写
   @abstractmethod
   def action(self):
       pass  # 抽象方法在父类通常留空,用pass进行占位

   
>>> ms=MySuper()
Traceback (most recent call last):
 File "
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分