0
我具有以下爲一個簡單的類(堆棧)代碼:運行命令
#lang racket
(define stackClass%
(class object%
(super-new)
(init-field (mystack '(A B C)))
(define/public (push n)
(set! mystack (cons n mystack)))
(define/public (pop)
(cond [(empty? mystack) #f]
[else (define res (car mystack))
(set! mystack (rest mystack))
res]))
(define/public (show)
mystack)
mystack ; I want to output mystack at time of creation of class object; not working here;
)); end class;
; USAGE:
(define sc (new stackClass%))
(send sc push 1)
(send sc push 2)
(send sc show)
(send sc pop)
(send sc show)
輸出:
'(2 1 A B C)
2
'(1 A B C)
我要輸出mystack在創建對象的類的時間。但是,它在這段代碼中不起作用,儘管沒有顯示錯誤。
使用以下代替(init-field (mystack '(A B C)))
也於事無補:
(init (L '(A B C)))
(define mystack L)
我怎麼能運行在類創建時的一段代碼?
我知道我錯過了很簡單的事情。謝謝。 – rnso