Advertisement
lisp123456

Untitled

Aug 16th, 2021
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. ;; Define Chair Class
  2.  
  3. (defclass chair ()
  4. ((color :accessor chair-color
  5. :initarg :color)
  6. (material :accessor chair-material
  7. :initarg :material)))
  8.  
  9. (defparameter *example* (make-instance 'chair
  10. :color "Gray"
  11. :material "Steel"))
  12.  
  13. (print *example*)
  14.  
  15. ;; Output >> #<CHAIR {10037C8CD3}>
  16.  
  17. (defmethod print-object ((obj chair) stream)
  18. (if (boundp '*print-method*)
  19. (print-unreadable-object (obj stream :type t)
  20. (format stream "Color: ~a~%" (chair-color obj))
  21. (format stream "Material: ~a" (chair-material obj)))
  22. (print "This is a cool chair")))
  23.  
  24. ;; Output >> "This is a cool chair"
  25.  
  26. (defun print-new-chair (color material)
  27. (let ((*print-method* t))
  28. (declare (special *print-method*))
  29. (print (make-instance 'chair :color color
  30. :material material))
  31. nil))
  32.  
  33.  
  34. (print-new-chair "Red" "Leather")
  35.  
  36. ;; Output >> #<CHAIR Color: Red
  37. ;; Material: Leather>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement