2014-06-18 6 views
3

입력 전압과 같은 전기 판독 값을 나타내는 개체를 만들고 싶습니다. 이를 위해 현재의 전압뿐만 아니라 다양한 종류의 판독 값을 처리하기위한 기본 클래스 구조를 만들고 싶습니다.Forth에서 클래스의 기본 클래스와 인스턴스를 어떻게 만듭니 까?

의사 코드는 (물론, 실제로 파이썬) 내가하고 싶은 것은이 때문이다 : 나는 Gforthoof.fs 확장자를 사용하고

# 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 기호와

답변

4

:

require oof.fs 

object class power-reading 
    float var value 
    cell var units 
    method . 
how: 
    : init (r-value units --) units ! value f! ; 
    : . (--) value [email protected] 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 

매우 유사, 그것은되지 않습니다

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

symbol 'volts 
symbol 'amps 

여기 파이썬의 oof.fs 동등한이야?

요즘에는 oof.fs보다 훨씬 낮은 레벨 인 mini-oof2.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 [email protected] 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>로 (일종의) 매크로 확장. 복잡한 객체 지향 및 SUPER 방법으로뿐만 아니라

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

그러나 물론이 재생되지 않습니다

보다는 INIT 방법이

, 나는 종종 객체를 구성하는 비 OO 단어가 등. oof.fs와 SWOOP 및 FMS가 제공하는 100 % OO 솔루션과는 대조적으로 90 % 솔루션입니다.

+0

불량 해답! 'oof.fs' 예제는 완벽하게 작동하고 내가 찾고있는 것을 정확히 수행합니다. gforth 기호의 사용도 좋습니다. 'mini-oof2.fs' 예제에 감사하지만'[: ...]] 구문을 사용할 수 없습니다 (': 10 : 정의되지 않은 단어, >>> [: <<< ... '). 이 동의어가 정의했거나 내장되어 있습니까? 참고로'require mini-oof.fs' (require mini-oof2.fs' backtraced)를 사용했습니다. – CivFan

+0

@CivFan, 기본 제공되며 기본 제공되지만 gforth의 최신 버전이 필요할 수 있습니다. 나는 안드로이드에서 gforth를 사용하고 있습니다. mini-oof.fs는 분명히 그 코드에서 작동하지 않을 것입니다 : 예를 들어'> o o o>'와 친구들을 사용하지 않습니다. –