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

时间:2019-12-17 18:30:19

标签: python class

我有两个班:信息和连接。连接应该继承Info。我的最终目标是使Connection能够访问Info的属性,以便对于Connection创建的每个实例,都将传递一个Info实例。然后Connection将有权访问此Info实例。

我一直在浏览论坛和教程,我理解这个概念,但是我尝试过的所有方法似乎都无法解决这个问题。我尝试使用super(),然后仅在Connection的__init__函数内调用Info的__init__无效。

作为测试,使用下面的代码,我尝试通过Connection类的实例(即slot_number)访问属性Connection.slot_number,但是没有用。即使Info显然有一个名为Info has no attribute 'slot_number'的属性,我仍然收到错误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

2 个答案:

答案 0 :(得分:0)

这是一个例子

NodeList

针对您的具体情况:

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())


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

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

答案 1 :(得分:0)

调用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的目的是支持多重继承,在这种情况下,您不必知道哪个类的__init__方法将由super调用。您可以做的最好的事情是接受任意关键字参数,使用您要做的知道的参数,然后传递其余的参数。这包括Info,即使您是从object静态地继承,因为您在定义Info时也不知道其的方法解析顺序(MRO) self将在运行时。