繁体   English   中英

如何在python中继承另一个类的属性

[英]How to inherit another class's attributes in python

我有两个类:信息和连接。 Connection 应该继承 Info。 我的最终目标是让 Connection 能够访问 Info 的属性,这样对于每个创建的 Connection 实例,都会传入一个 Info 实例。然后 Connection 将可以访问这个 Info 属性的实例。

我一直在浏览论坛和教程,我理解这个概念,但我尝试过的一切似乎都没有让它起作用。 我试过使用super()然后在 Connection 的__init__函数中调用 Info 的__init__无济于事。

作为测试,使用下面的代码,我尝试通过 Connection 类的实例(即Connection.slot_number )访问属性slot_number但它没有工作。 我收到错误Info has no attribute 'slot_number'即使 Info 显然有一个名为slot_number的属性。 谁能给我一些关于如何做到这一点的澄清?

class Info(object):
    def __init__(self, slot_number, board_type, board_part_num, prod_part_num):
        self.slot_number = slot_number
        self.board_type = board_type
        self.board_part_num = board_part_num
        self.prod_part_num = prod_part_num

class Connection(Info):
    def __init__(self, system, sn, phase, ip, port):
        super(Info, self).__init__()
        self.system = system
        self.sn = sn
        self.slot_number = slot_number
        self.phase = phase
        self.ip = ip
        self.port = port

这是一个例子

class Person():
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def getName(self):
    return self.name
  def getAge(self):
    return self.age


class Student(Person):
  def __init__(self, name, age, id):
    self.id = id
    super().__init__(name, age)

  def getId(self):
    return self.id

new_student = Student('Student', 20, 666)

print(new_student.getName())


至于你的具体情况:

class Info(object):
    def __init__(self, slot_number, board_type, board_part_num, prod_part_num):
        self.slot_number = slot_number
        self.board_type = board_type
        self.board_part_num = board_part_num
        self.prod_part_num = prod_part_num

     def getSlotNumber():
          return self.slot_number

class Connection(Info):
    def __init__(self, system, sn, phase, ip, port, slot_number, board_type, board_part_num, prod_part_num):
        super().__init__(slot_number, board_type, board_part_num, prod_part_num)
        self.system = system
        self.sn = sn
        self.slot_number = slot_number
        self.phase = phase
        self.ip = ip
        self.port = port

您可以通过创建 get 方法来访问 Info 的属性。

new_connection = Connection('system', 'phase', 'ip', 'port', 'slot_number', 'board_type', 'board_part_num', 'prod_part_num')
new_connection.getSlotNumber()

当您调用super().__init__ ,您需要传递预期的值。 您要么需要对它们进行硬编码

class Info(object):
    def __init__(self, slot_number, board_type, board_part_num, prod_part_num):
        self.slot_number = slot_number
        self.board_type = board_type
        self.board_part_num = board_part_num
        self.prod_part_num = prod_part_num

class Connection(Info):
    def __init__(self, system, sn, phase, ip, port):
        super().__init__(0, "foo", 3, 9)
        self.system = system
        self.sn = sn
        self.phase = phase
        self.ip = ip
        self.port = port

c = Connection(arg1, arg2, arg3, arg4, arg5)

或者将它们传递给Connection.__init__以便它们可以传递。

class Info(object):
    def __init__(self, slot_number, board_type, board_part_num, prod_part_num, **kwargs):
        super().__init__(**kwargs)
        self.slot_number = slot_number
        self.board_type = board_type
        self.board_part_num = board_part_num
        self.prod_part_num = prod_part_num

class Connection(Info):
    def __init__(self, system, sn, phase, ip, port, **kwargs):
        super().__init__(*kwargs)
        self.system = system
        self.sn = sn
        self.phase = phase
        self.ip = ip
        self.port = port

c = Connection(system=arg1,
               sn=arg2,
               phase=arg3,
               ip=arg4,
               port=arg5,
               slot_number=arg6,
               board_type=arg7,
               board_part_num=arg8,
               prod_part_num=arg9)

第二种方法更可取,因为super的目的是支持多重继承,在这种情况下,您不一定知道super将调用哪个类的__init__方法。 你能做的最好的事情是接受任意关键字参数,使用知道的参数,并通过对休息。 这包括Info ,即使您是从object静态继承的,因为您不知道在定义Infoself的方法解析顺序 (MRO) 在运行时将是什么。

暂无
暂无

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

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