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 秒) - 正常终止