【发布时间】:2019-05-31 11:54:00
【问题描述】:
我需要在泛型方法中返回一个矩形的坐标列表。坐标是类“购物车”实例。
我尝试用 make-instance 返回它
(defclass line ()
((start :initarg :start :accessor line-start)
(end :initarg :end :accessor line-end)))
(defmethod print-object ((lin line) stream)
(format stream "[LINE ~s ~s]"
(line-start lin) (line-end lin)))
(defclass cart ()
((x :initarg :x :reader cart-x)
(y :initarg :y :reader cart-y)))
(defmethod print-object ((c cart) stream)
(format stream "[CART x ~d y ~d]"
(cart-x c) (cart-y c)))
(setq lin (make-instance 'line
:start (make-instance 'cart :x 4 :y 3)
:end (make-instance 'cart :x 7 :y 5)))
(defgeneric containing-rect (shape))
(defmethod containing-rect ((l line))
(let ((x1 (cart-x (line-start l)))
(y1 (cart-y (line-start l)))
(x2 (cart-x (line-end l)))
(y2 (cart-y (line-end l))))
(cond ((= x1 x2)
'((make-instance 'cart :x (1- x1) :y y1)
(make-instance 'cart :x (1+ x1) :y y1)
(make-instance 'cart :x (1- x2) :y y2)
(make-instance 'cart :x (1+ x2) :y y2)))
((= y1 y2)
'((make-instance 'cart :x x1 :y (1- y1))
(make-instance 'cart :x x1 :y (1+ y1))
(make-instance 'cart :x x2 :y (1- y2))
(make-instance 'cart :x x2 :y (1+ y2))))
(t
(rect '((make-instance 'cart :x x1 :y y1)
(make-instance 'cart :x x1 :y y2)
(make-instance 'cart :x x2 :y y2)
(make-instance 'cart :x x2 :y y1)))))))
(print (containing-rect lin))
我想make-instance 应该为某事分配一个实例
所以我得到了不正确的结果
((MAKE-INSTANCE 'CART :X X1 :Y Y1) (MAKE-INSTANCE 'CART :X X1 :Y Y2)
(MAKE-INSTANCE 'CART :X X2 :Y Y2) (MAKE-INSTANCE 'CART :X X2 :Y Y1))
但我需要这样的输出
([CART x 4 y 3] [CART x 4 y 3] [CART x 4 y 3] [CART x 4 y 3])
【问题讨论】:
-
为什么不调用
lin变量line?
标签: common-lisp evaluation quote