खोज…


माता-पिता के बिना एक मूल CLOS क्लास बनाना

एक CLOS वर्ग द्वारा वर्णित है:

  • एक नाम
  • सुपरक्लास की एक सूची
  • स्लॉट की एक सूची
  • प्रलेखन जैसे अन्य विकल्प

प्रत्येक स्लॉट में है:

  • एक नाम
  • एक प्रारंभिक रूप (वैकल्पिक)
  • एक प्रारंभिक तर्क (वैकल्पिक)
  • एक प्रकार (वैकल्पिक)
  • एक प्रलेखन स्ट्रिंग (वैकल्पिक)
  • एक्सेसर, रीडर और / या लेखक फ़ंक्शन (वैकल्पिक)
  • आगे के विकल्प जैसे आवंटन

उदाहरण:

(defclass person ()
  ((name
    :initform      "Erika Mustermann" 
    :initarg       :name 
    :type          string
    :documentation "the name of a person"
    :accessor      person-name)
   (age
    :initform      25
    :initarg       :age
    :type          number
    :documentation "the age of a person"
    :accessor      person-age))
  (:documentation "a CLOS class for persons with name and age"))

एक डिफ़ॉल्ट प्रिंट विधि:

(defmethod print-object ((p person) stream)
  "The default print-object method for a person"
  (print-unreadable-object (p stream :type t :identity t)
    (with-slots (name age) p
      (format stream "Name: ~a, age: ~a" name age))))

उदाहरण बनाना:

CL-USER > (make-instance 'person)
#<PERSON Name: Erika Mustermann, age: 25 4020169AB3>

CL-USER > (make-instance 'person :name "Max Mustermann" :age 24)
#<PERSON Name: Max Mustermann, age: 24 4020169FEB>

मिश्रण और इंटरफेस

आम लिस्प में इस अर्थ में इंटरफेस नहीं होता है कि कुछ भाषाएं (जैसे, जावा) करती हैं, और उस प्रकार के इंटरफ़ेस की कम आवश्यकता होती है, जिसे देखते हुए कॉमन लिस्प कई विरासत और सामान्य कार्यों का समर्थन करता है। हालांकि, मिक्सिन वर्गों का उपयोग करके समान प्रकार के पैटर्न को आसानी से महसूस किया जा सकता है। यह उदाहरण कई संबंधित सामान्य कार्यों के साथ एक संग्रह इंटरफ़ेस की विशिष्टता को दर्शाता है।

;; Specification of the COLLECTION "interface"

(defclass collection () ()
  (:documentation "A collection mixin."))

(defgeneric collection-elements (collection)
  (:documentation "Returns a list of the elements in the collection."))

(defgeneric collection-add (collection element)
  (:documentation "Adds an element to the collection."))

(defgeneric collection-remove (collection element)
  (:documentation "Removes the element from the collection, if it is present."))

(defgeneric collection-empty-p (collection)
  (:documentation "Returns whether the collection is empty or not."))

(defmethod collection-empty-p ((c collection))
  "A 'default' implementation of COLLECTION-EMPTY-P that tests
whether the list returned by COLLECTION-ELEMENTS is the empty
list."
  (endp (collection-elements c)))

इंटरफ़ेस का एक कार्यान्वयन सिर्फ एक वर्ग है जिसमें मिक्सिन अपने सुपर क्लास में से एक है, और उचित सामान्य कार्यों की परिभाषाएं हैं। (इस बिंदु पर, ध्यान दें कि मिक्सिन वर्ग वास्तव में केवल इस आशय के संकेत के लिए है कि वर्ग "इंटरफ़ेस" लागू करता है। यह उदाहरण केवल कुछ सामान्य कार्यों और प्रलेखन के साथ काम करेगा जो बताता है कि फ़ंक्शन के लिए तरीके हैं। कक्षा।)

;; Implementation of a sorted-set class

(defclass sorted-set (collection)
  ((predicate
    :initarg :predicate
    :reader sorted-set-predicate)
   (test
    :initarg :test
    :initform 'eql
    :reader sorted-set-test)
   (elements
    :initform '()
    :accessor sorted-set-elements
    ;; We can "implement" the COLLECTION-ELEMENTS function, that is,
    ;; define a method on COLLECTION-ELEMENTS, simply by making it
    ;; a reader (or accessor) for the slot.
    :reader collection-elements)))

(defmethod collection-add ((ss sorted-set) element)
  (unless (member element (sorted-set-elements ss)
                  :test (sorted-set-test ss))
    (setf (sorted-set-elements ss)
          (merge 'list
                 (list element)
                 (sorted-set-elements ss)
                 (sorted-set-predicate ss)))))

(defmethod collection-remove ((ss sorted-set) element)
  (setf (sorted-set-elements ss)
        (delete element (sorted-set-elements ss))))

अंत में, हम देख सकते हैं कि "इंटरफ़ेस" फ़ंक्शन का उपयोग करते समय सॉर्ट किए गए सेट वर्ग की एक आवृत्ति का उपयोग क्या दिखता है:

(let ((ss (make-instance 'sorted-set :predicate '<)))
  (collection-add ss 3)
  (collection-add ss 4)
  (collection-add ss 5)
  (collection-add ss 3)
  (collection-remove ss 5)
  (collection-elements ss))
;; => (3 4)


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow