繁体   English   中英

如何继承派生类的子类形式基 class?

[英]How to inherit derived class's subclass form base class?

我有的是这段代码

 class A: ''' merge all services together ''' def __init__(self): self.event = 'event' '''It includes all common method to all services''' # TODO inherit these class in others class B: def __init__(self): self.event = 'event' def get_event_type(self): self.event = 'event' class C(B): def __init__(self): self.event = 'event' class D(B): def __init__(self): self.event = 'event' def get_job_id(self): self.event = 'event'

在这里我想用 B 继承 class D 但它出现错误

 Traceback (most recent call last): File "<string>", line 1, in <module> File "<string>", line 16, in A File "<string>", line 21, in C NameError: name 'B' is not defined

因此,如果有人可以帮助我解决这个问题,为什么我会收到此错误? 即使我继承了 class C。

我认为这是一个缩进问题你可以试试:

class A:
    ''' merge all services together '''
    def __init__(self):
        self.event = 'event'
    
'''It includes all common method to all services'''
# TODO inherit these class in others
class B:
    def __init__(self):
        self.event = 'event'

    def get_event_type(self):
        self.event = 'event'


class C(B):
    def __init__(self):
        self.event = 'event'


class D(B):
    def __init__(self):
        self.event = 'event'


    def get_job_id(self):
       self.event = 'event'

暂无
暂无

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

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