Python 继承后使用抽象方法
使用pyide。 我想将两个抽象类A+B继承到一个新的类C中。我想实现一个类的方法,并使用另一个类的方法,该方法在后面的类E中实现,在A在D中实现 我的问题是:当创建C类时,intelliJ希望我实现所有方法,所以类A的方法和类B的方法。我只想实现类A方法,但需要类B的“接口”来知道哪些方法对我可用。有没有办法告诉intelliJ我不想在新类中实现特定的类abstractmethods 甚至-有没有更好的方式来完成这一切Python 继承后使用抽象方法,python,inheritance,intellij-idea,pycharm,multiple-inheritance,Python,Inheritance,Intellij Idea,Pycharm,Multiple Inheritance,使用pyide。 我想将两个抽象类A+B继承到一个新的类C中。我想实现一个类的方法,并使用另一个类的方法,该方法在后面的类E中实现,在A在D中实现 我的问题是:当创建C类时,intelliJ希望我实现所有方法,所以类A的方法和类B的方法。我只想实现类A方法,但需要类B的“接口”来知道哪些方法对我可用。有没有办法告诉intelliJ我不想在新类中实现特定的类abstractmethods 甚至-有没有更好的方式来完成这一切 class A(metaclass=ABCMeta): """in
class A(metaclass=ABCMeta):
"""interface class"""
@abstractmethod
def click(self, c): pass
class B(metaclass=ABCMeta):
"""interface class"""
@abstractmethod
def do_something(self): pass
class C(A, B):
# implement methods of B here, use abstract methods of A
# as interface to know what methods are available for use
def do_something(self):
self.click("anything")
class D(A):
# implement A
def click(self, c):
print("I clicked {}".format(c))
class E(C,D):
# merge implementations to create runnable `do_something()`
def main():
self.do_something()
E.main()
如前所述,您的类
C
仍然是抽象的,因为它没有实现A
中的抽象方法,所以应该将其标记为:
class C(A, B, metaclass=ABCMeta):
# implement methods of B here, use abstract methods of A
# as interface to know what methods are available for use
def do_something(self):
self.click("anything")
您可以简单地抑制检查:
#noinspection PyAbstractClass
我已经考虑过了,但是当C声明为ABCMeta时,有没有办法仍然强制B的所有抽象方法在C中实现呢?没有(至少不是直接的)-但是当您掌握了代码时,为什么还需要这个呢?无论如何:你确定多重继承是正确的解决方案吗?也许组合+委托会更有意义?我想把代码交给其他人来贡献,并需要他们拥有A的接口,但实现B的所有方法。也是的,我将重新考虑多重继承的解决方案,并尝试另一种方法。谢谢你的意见