在Python中,子类可以继承父类的属性和方法。如果你想在子类中使用父类的属性,可以使用super()函数来调用父类的方法并获取父类的属性。例如:
class Parent: def __init__(self, name): self.name = nameclass Child(Parent): def __init__(self, name, age): super().__init__(name) self.age = ageparent = Parent("Alice")child = Child("Bob", 10)print(parent.name) # 输出:Aliceprint(child.name) # 输出:Bobprint(child.age) # 输出:10在上面的例子中,子类Child继承了父类Parent的属性name,并通过调用super().__init__(name)来初始化父类的属性。


