简体   繁体   English

Python中具有接口和继承的多层抽象

[英]Multilevel abstraction with interface and inheritance in Python

I'm not exactly sure how to phrase this question, hence the strange title. 我不确定如何表达这个问题,因此标题很奇怪。 I also have not been able to find any information on this after searching, so hopefully this isn't a duplicate and I'm just searching for the wrong words. 搜索后,我也无法找到任何有关此的信息,因此希望这不是重复的,而我只是在搜索错误的单词。 Anyhow, here is the situation, I have an abstract base class with some methods in it, which is inherited by a class. 无论如何,在这种情况下,我有一个抽象基类,其中包含一些方法,该基类由类继承。 I don't want to set one of the methods in this base class, as this class is meant to be inherited by other classes to provide the common functionality they all share. 我不想在该基类中设置方法之一,因为该类是要被其他类继承以提供它们都共享的通用功能。 Something like: 就像是:

class A(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def fun1(self):
        pass

   @abc.abstractmethod
    def fun2(self):
        pass

class B(A):
    def fun1(self):
        #do work here

    @abc.abstractmethod
    def fun2(self):  # Intent to have the final classes define this
        pass

class C(B):
    def fun2(self):
        # do work here    

class D(B):
    def fun2(self):
        # do work here    

I would like to keep the function as an ABC.meta to force implementation on the final children, but because there can be multiple types of class B in this case all inheriting from the interface, I want to keep the initial virtulization of the method at this root class, but have a way for class B to enforce that it's sub-classes must implement this. 我想将该函数保留为ABC.meta,以强制在最终子级上实现,但是由于在这种情况下可以有多种类型的B类全部从接口继承,因此我想将该方法的初始虚拟化保留在这个根类,但是B类必须强制它的子类必须实现此方法。 The code works just find if I don't add the abstract method to class B, but that is awkward since subclassess must implement the method and shouldn't have to look all the way up to the interface to figure out everything they need to implement. 代码可以找到是否没有在类B中添加抽象方法,但是这样做很尴尬,因为子类必须实现该方法,并且不必从头到尾查看接口来找出他们需要实现的所有内容。 As written, it will error out because class B cannot declare the method as an abc.abstract. 如所写,它将出错,因为类B无法将方法声明为abc.abstract。 If I don't declare it as an abstract there is no way to enforce the child class has to implement the method. 如果我没有将其声明为抽象,则无法强制子类实现该方法。

I hope my convoluted way of writing this makes sense to someone out there... 我希望我那费解的书写方式对那里的某人有意义...

Thanks! 谢谢!

You probably should not redefine fun2 as an abstract method in the concrete class B . 您可能不应在具体的类B中将fun2重新定义为抽象方法。 You are creating a set of rules for your interface, but immediately violating them when you do that. 您正在为接口创建一组规则,但是在这样做时会立即违反它们。

Instead, either define a mix-in class or an additional ABC that C and D can inherit. 而是定义一个混合类或CD可以继承的其他ABC。

class A(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def fun1(self):
        pass

class A2(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def fun2(self):
        pass

class B(A):
    def fun1(self):
        print('hello')

class B2(A2):
    def fun2(self):
        print('world')

class C(B, B2):
    pass

class D(B, B2):
    pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM