3

我想创建一个对象来表示一些电气读数,例如输入电压。为此,我想创建一个基本的类结构来处理不同类型的读数——比如电流和电压。

我想要做的伪代码(实际上是 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

我正在使用Gforthoof.fs扩展。

4

1 回答 1

4

使用一些简单的 gforth 符号:

: symbol ( "name" -- )
  create lastxt , does> ( -- xt ) @ ;
: .symbol ( xt -- )
  >name name>string 1 /string type ;

symbol 'volts
symbol 'amps

这是你的 Python 的 oof.fs 等价物:

require oof.fs

object class power-reading
  float var value
  cell var units
  method .
how:
  : init ( r-value units -- )  units !  value f! ;
  : . ( -- ) value f@ f.  units @ .symbol space ;
class;

power-reading class voltage
how:
  : init ( r-value -- )  'volts super init ;
class;

power-reading class current
how:
  : init ( r-value -- )  'amps super init ;
class;

3.25e voltage : input-voltage
input-voltage .  \ Output: 3.25 volts

这很相似,不是吗?

这些天我使用 mini-oof2.fs,它比 oof.fs 级别低得多,而且做的也少很多。在那里面:

object class
  ffield: value
  field: units
  method init ( value units -- )
  method show ( -- )
end-class power-reading

[: ( r-value units -- )  units !  value f! ;] power-reading to init
[: ( -- ) value f@ f.  units @ .symbol space ;] power-reading to show

power-reading class
end-class voltage

[: ( r-value -- )  value f! 'volts units ! ;] voltage to init

power-reading class
end-class current

[: ( r-value -- )  value f! 'amps units ! ;] current to init

voltage new constant input-voltage
3.25e input-voltage .init
input-voltage .show  \ Output: 3.25 volts

[: ... ;]不是特殊的 mini-oof2.fs 语法。它们只是 gforth 引用,在这里用作 :NONAME 的同义词。

和宏分别扩展(某种程度) 为.init和。.show>o init o>>o show o>

我经常使用非 OO 词来构造对象,而不是使用 INIT 方法:

: >current ( r -- o )
  current new >o  value f!  'amps units !  o o> ;
: current, ( r -- )
  here >o [ current >osize @ ]L allot  value f!  'amps units !  o> ;

但当然,这在复杂的面向对象和超级方法等方面效果不佳。这是一个 90% 的解决方案,而不是 oof.fs 和 SWOOP 和 FMS 提供的 100% OO 解决方案。

于 2014-06-24T23:10:25.303 回答