面试之面向对象之约束
1.什么是接口以及作用:
答:接口是一种数据类型,主要约束派生类中必须实现指定的方法
python 中不存在,java,c#中存在
2.python 中使用过什么来约束呢?
答:第一个是抽象类+抽象方法: 编写上麻烦
人为上主动抛出异常
3.约束时,抛出的异常可以是其他的吗?
不专业:Raise Exception()
专业: Raise NotImplementError()
class Base: def send(self): raise NotImplementedError('send 必须被重写') class Foo(Base): def send(self): print('send必须被重写') obj =Foo() obj.send()
from abc import ABCMeta,abstractmethod class Base(metaclass= ABCMeta): def f1(self): print() @abstractmethod def send(self): raise NotImplementedError('send 必须被重写') class Foo(Base): def send(self): print('send必须被重写') obj =Foo() obj.send()

浙公网安备 33010602011771号