注意:它在python中。简化的代码是,
class main:
def __init__(self):
pass
def unneededfunction(self):
print("unneeded thing")
class notmain(main):
def __init__(self):
pass
#code for getting rid of unneededfunction here
问题是,你如何做到使 notmain.unneededfunction 不存在(即调用它会导致错误)?
如果你不想notmain
拥有unneededfunction
那么notmain
不应该是main
. 以这种方式对抗系统,这完全违背了继承的观点。
如果你真的坚持这样做,notmain
可以重新定义unneededfunction
和引发如果unneededfunction
不存在就会引发的相同异常,AttributeError
. 但是,你又违背了常规。
除此之外,您不能删除unneededfunction
fromnotmain
因为notmain
它不拥有该方法,它的父类拥有main
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句