--sumim(defclass bank-account () ((dollars :accessor dollars :initform 0 :initarg :dollars))) (defmethod set-dollars ((self bank-account) (x number)) (setf (dollars self) x)) (defmethod deposit ((self bank-account) (x number)) (set-dollars self (+ (dollars self) x))) (defmethod withdraw ((self bank-account) (x number)) (set-dollars self (max 0 (- (dollars self) x)))) (setq my-account (make-instance 'bank-account :dollars 200)) (dollars my-account) ==> 200 (deposit my-account 50) ==> 250 (withdraw my-account 100) ==> 150 (withdraw my-account 200) ==> 0 (defclass stock-account (bank-account) ((num-shares :accessor num-shares :initform 0 :initarg :num-shares) (price-per-share :accessor price-per-share :initform 30 :initarg :price-per-share))) (defmethod set-num-shares ((self stock-account) (x number)) (setf (num-shares self) x)) (defmethod set-price-per-share ((self stock-account) (x number)) (setf (price-per-share self) x)) (defmethod set-dollars ((self stock-account) (x number)) (set-num-shares self (/ (float x) (price-per-share self))) (dollars self)) (defmethod dollars ((self stock-account)) (* (num-shares self) (price-per-share self))) (setq my-stock (make-instance 'stock-account :num-shares 10)) (dollars my-stock) ==> 300 (set-dollars my-stock 600) ==> 600.0 (deposit my-stock 60) ==> 660.0 (num-shares my-stock) ==> 22.0 (withdraw my-stock 120) ==> 540.0 (num-shares my-stock) ==> 18.0
このページを編集 (2604 bytes)
以下の 3 ページから参照されています。 |
This page has been visited 4296 times.