如何通过继承和 super 关键字将参数传递给父 class?
How to pass arguments to parent class with inheritance and with super keyword?
我的程序如下图:
class BioData:
def __init__(self, FirstName, LastName):
self.FirstName = FirstName
self.LastName = LastName
class BioData_OP(BioData):
def __init__(self, age, address):
super().__init__(FirstName,LastName)
self.age = age
self.address = address
mydata = BioData_OP(36,"CNGF","Big","Bee")
我收到一个错误。我知道如果我传递两个参数,它不会给我一个错误,但在那种情况下我如何获得名字和姓氏?
我不想在 class BioData_OP
中初始化它们的值。我需要为 BioData 创建一个单独的对象吗?如果是,那么使用继承或 super
关键字的好处是什么。
继承的要点是表明某个class(例如Dog
)与父是一个”关系class(例如 Animal
)。因此,Dog
是 Animal
,但具有只有狗才有的某些特性(或仅适用于狗)。
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
a_dog = Dog("woolfie", "labrador")
在这里,__init__
仍然采用 name
,但还采用了狗的额外属性,例如 breed
。对于公共属性 name
,您可以通过 super
.
将其传递给父 class
因此,将其应用于您的代码,我假设 BioData_OP
对象 是 BioData
的 类型,但添加了 age
和 address
属性。 __init__
可能仍然接受相同的名字和姓氏,但只是为 age
和 address
添加了参数。
class BioData:
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
class BioData_OP(BioData):
def __init__(self, first_name, last_name, age, address):
super().__init__(first_name, last_name)
self.age = age
self.address = address
mydata = BioData_OP("Big","Bee", 36,"CNGF")
,根据您的预期 use-case 或情况,如果这将成为工厂设计模式的一部分,您应该确保不要更改基础 class'初始化,而是让您的 subclass 接受 subclass-specific 值的可选值或提供单独的方法来设置这些值。
class BioData:
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
class BioData_OP(BioData):
def __init__(self, first_name, last_name, age=-1, address=None):
super().__init__(first_name, last_name)
self.age = age
self.address = address
def update(self, age, address):
self.age = age
self.address = address
# Still works
# Will create a BioData_OP object but without age and address
# Basically creates a BioData object
mydata_1 = BioData_OP("Big","Bee")
# Directly create a BioData_OP object
mydata_2 = BioData_OP("Big","Bee", 36,"CNGF")
# First, create a BioData object
# Then, turn in into a BioData_OP object
mydata_3 = BioData_OP("Big","Bee")
mydata_3.update(36, "CNGF")
我的程序如下图:
class BioData:
def __init__(self, FirstName, LastName):
self.FirstName = FirstName
self.LastName = LastName
class BioData_OP(BioData):
def __init__(self, age, address):
super().__init__(FirstName,LastName)
self.age = age
self.address = address
mydata = BioData_OP(36,"CNGF","Big","Bee")
我收到一个错误。我知道如果我传递两个参数,它不会给我一个错误,但在那种情况下我如何获得名字和姓氏?
我不想在 class BioData_OP
中初始化它们的值。我需要为 BioData 创建一个单独的对象吗?如果是,那么使用继承或 super
关键字的好处是什么。
继承的要点是表明某个class(例如Dog
)与父是一个”关系class(例如 Animal
)。因此,Dog
是 Animal
,但具有只有狗才有的某些特性(或仅适用于狗)。
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
a_dog = Dog("woolfie", "labrador")
在这里,__init__
仍然采用 name
,但还采用了狗的额外属性,例如 breed
。对于公共属性 name
,您可以通过 super
.
因此,将其应用于您的代码,我假设 BioData_OP
对象 是 BioData
的 类型,但添加了 age
和 address
属性。 __init__
可能仍然接受相同的名字和姓氏,但只是为 age
和 address
添加了参数。
class BioData:
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
class BioData_OP(BioData):
def __init__(self, first_name, last_name, age, address):
super().__init__(first_name, last_name)
self.age = age
self.address = address
mydata = BioData_OP("Big","Bee", 36,"CNGF")
class BioData:
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
class BioData_OP(BioData):
def __init__(self, first_name, last_name, age=-1, address=None):
super().__init__(first_name, last_name)
self.age = age
self.address = address
def update(self, age, address):
self.age = age
self.address = address
# Still works
# Will create a BioData_OP object but without age and address
# Basically creates a BioData object
mydata_1 = BioData_OP("Big","Bee")
# Directly create a BioData_OP object
mydata_2 = BioData_OP("Big","Bee", 36,"CNGF")
# First, create a BioData object
# Then, turn in into a BioData_OP object
mydata_3 = BioData_OP("Big","Bee")
mydata_3.update(36, "CNGF")