这是我创建的一个类,用于与名为“Waveplate”的设备进行通信。 “Waveplate”类继承serial.Serial Baseclass的属性和方法。但是如您所见,需要初始化基类serial.Serial。我在下面所做的工作,我的问题是,这是最优雅的方式吗?
FOOD *my_food = malloc(sizeof(FOOD));
if (my_food != NULL)
{
my_food->category = malloc(256);
if (my_food->category != NULL)
{
// USAGE
}
}
答案 0 :(得分:0)
您应该使用super
:
在python 3中:
class WavePlate(serial.Serial):
""" This class returns an instance that contains the attributes and methods of a ``serial.Serial`` object.
"""
def __init__(self, p, brate):
"""Here's the constructor. """
self.p = p
self.brate = brate
super().__init__(port=self.p, baudrate=self.brate, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE)
在python 2中:
class WavePlate(serial.Serial):
""" This class returns an instance that contains the attributes and methods of a ``serial.Serial`` object.
"""
def __init__(self, p, brate):
"""Here's the constructor. """
self.p = p
self.brate = brate
super(WavePlate, self).__init__(port=self.p, baudrate=self.brate, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE)
注意:大多数情况下,您希望在子构造函数中进行任何声明之前调用超类构造函数。