如何在Forth中创建该类的基本类和实例?

时间:2014-06-18 20:26:15

标签: class oop object forth gforth

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

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

1 个答案:

答案 0 :(得分: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>分别。

我没有使用INIT方法,而是经常使用非OO字 构造对象:

: >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%的解决方案,而不是100% 由oof.fs和SWOOP以及FMS提供的OO解决方案。