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 構文は特別なものではありません。それらはただの引用であり、ここでは :NONAME の同義語として使用されています。

およびは、それぞれ.initおよびに.showマクロ展開 (並べ替え) され>o init o> ます>o show o>

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> ;

しかしもちろん、これは複雑なオブジェクト指向や SUPER メソッドなどではうまく機能しません。oof.fs、SWOOP、FMS によって提供される 100% OO ソリューションとは対照的に、これは 90% ソリューションです。

于 2014-06-24T23:10:25.303 に答える