python的抽象类详解_Python模拟抽象类

0 )

Python中没有abstract关键字定义抽象类,但是可以通过NotImplementedError类模拟抽象类

def abstract ():

raise NotImplementedError("Abstract")

class Person:

def __init__ (self):

if self.__class__ is Person:

abstract()

class Student(Person):

def __init__ (self):

Person.__init__(self)

print ("I am student")

stu = Student()

per = Person()

---------- Python ----------

I am student #这行输出了

Traceback (most recent call last):

File "aa.py", line 19, in

per = Person()

File "aa.py", line 10, in __init__

abstract()

File "aa.py", line 5, in abstract

raise NotImplementedError("Abstract")

NotImplementedError: Abstract #直接实例化Person类会报错

输出完毕 (耗时 0 秒) - 正常终止

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值