如何删除子类中的继承函数?

菜鸟匿名

注意:它在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 不存在(即调用它会导致错误)?

sj95126

如果你不想notmain拥有unneededfunction那么notmain不应该是main. 以这种方式对抗系统,这完全违背了继承的观点。

如果你真的坚持这样做,notmain可以重新定义unneededfunction和引发如果unneededfunction不存在就会引发的相同异常AttributeError. 但是,你又违背了常规。

除此之外,您不能删除unneededfunctionfromnotmain因为notmain它不拥有该方法,它的父类拥有main

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章