Я хотел бы создать объект для представления некоторых электрических показаний, таких как входное напряжение. Для этого я хочу создать базовую структуру классов для обработки различных типов показаний, например, тока и напряжения.
Псевдокод (ну, на самом деле, 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
.