下為實驗中的Forth.js OOP指令用法,
或是
到此下載(forthjstest1.zip )測試檔
class Vehicle weight power travelon end_class
instance Vehicle MyBike
20 to MyBike.weight
new MyCar
2000 MyCar price
MyCar.price
1000 +to MyCar.price
: move bra bra bra ;
assign move MyCar.move
:method MyCar.stop bra bra bra ;
以下測試案例,運用see指令,透視每個新建類別(class),物件(object),屬性(attribute)及方法(method)的內部
Example: OOP
<p class="forth" forthcode=' .( (Test1 define class ) ) class Vehicle weight power travelon end_class see Vehicle '>nil</p>
(Test1 define class )
Scanning Vehicle
(000113) doInstanceAllAttr
(000114) weight
(000115) power
(000116) travelon
(000117) exit
<p class="forth" forthcode=' .( (Test2 class instance ) ) instance Vehicle MyBike see MyBike see MyBike.weight 20 to MyBike.weight see MyBike.weight'>nil</p>
(Test2 class instance )
Scanning MyBike
(000118) doAttr
(000119) MyBike
(000120) exit
Scanning MyBike.weight
(000121) doValue
(000122) 0
(000123) exit
Scanning MyBike.weight
(000121) doValue
(000122) 20
(000123) exit
<p class="forth" forthcode=' .( (Test3 new object) ) new MyCar see MyCar 2000 MyCar price see MyCar.price cr MyCar.price . 1000 to MyCar.price MyCar.price . '>nil</p>
(Test3 new object)
Scanning MyCar
(000130) doAttr
(000131) MyCar
(000132) exit
Scanning MyCar.price
(000133) doValue
(000134) 2000
(000135) exit
2000 1000
<p class="forth" forthcode=' .( (Test4 object method) ) : move ." Just Moving!!!" ; see move assign move MyCar.move see MyCar.move cr cr MyCar.move cr see MyCar'>nil</p>
(Test4 object method)
Scanning move
(000136) (.")
(000137) Just Moving!!!
(000138) exit
Scanning MyCar.move
(000136) (.")
(000137) Just Moving!!!
(000138) exit
Just Moving!!!
Scanning MyCar
(000130) doAttr
(000131) MyCar
(000132) exit
<p class="forth" forthcode=' .( (Test5 object method) ) :method MyCar.stop ." Just Stopping!!!" ; see MyCar.stop cr cr MyCar.stop cr cr see MyCar'>nil</p>
(Test5 object method)
Scanning MyCar.stop
(000139) (.")
(000140) Just Stopping!!!
(000141) exit
Just Stopping!!!
Scanning MyCar
(000130) doAttr
(000131) MyCar
(000132) exit