约束
#示例一
class Foo(object):
def send(self):
print("yang")
class Base(object):
pass
def func(arg):
arg.send()#调用的是参数的一个方法
if 1 == 1:
func(Foo())
#示例二
class Interface(object):
def send(self):
raise NotImplemented()#抛出一个错误
class Message(Interface):#老版继承了Interface
def send(self):
print("发送短息")
class Email(Interface): #新版也要继承Interface
def send(self):
print("发送邮件")
#约束子类中必须写send方法,如果不写,则调用的时候就报抛出NotImplemented(子类没有实现指定方法所报的异常)
#约束应用案例
class BaseMessage(object):
def send(self,a1):
raise NotImplemented("子类中必须有send方法")
class Msg(BaseMessage):
def send(self):
pass
class Email(BaseMessage):
def send(self):
pass
class wetchat(BaseMessage):
def send(self):
pass
obj = Email()
obj.send