我想创建一个对象来表示一些电气读数,例如输入电压。为此,我想创建一个基本的类结构来处理不同类型的读数——比如电流和电压。
我想要做的伪代码(实际上是 Python)是这样的:
# Create base class as a subclass of a common class to all other classes
class PowerReading(object):
# Defining word to initialize instance variables using the given input
def __init__(self, current_value, units):
# instance variables
self.value = current_value
self.units = units
# Define new class based on our generic class above
class Voltage(PowerReading):
# Call the parent class word with an input value, and constant units string
def __init__(self, current_value):
super(Voltage, self).__init__(current_value, 'volts')
# Create another class based on the same parent class as Voltage
class Current(PowerReading):
def __init__(self, current_value):
# Call the parent word with current units
super(Voltage, self).__init__(current_value, 'amps')
# input_voltage_atod() is defined elsewhere: gives an instant reading
# from the ATOD pin on the power input rail, already converted to units of volts.
# Create instance object variable using our new Voltage class.
input_voltage = Voltage(input_voltage_atod())
# Use the object's instance variables
print input_voltage.value, input_voltage.units
# 3.25 volts
我正在使用Gforth
和oof.fs
扩展。