面试之面向对象之约束

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()

 

posted @ 2021-01-27 12:57  苦行僧冬*婷  阅读(39)  评论(0)    收藏  举报