LISP - CLOS

Le LISP commun a précédé les progrès de la programmation orientée objet de quelques décennies. Cependant, l'orientation objet y a été incorporée à un stade ultérieur.

Définition des classes

le defclassmacro permet de créer des classes définies par l'utilisateur. Il établit une classe comme type de données. Il a la syntaxe suivante -

(defclass class-name (superclass-name*)
   (slot-description*)
   class-option*))

Les emplacements sont des variables qui stockent des données ou des champs.

Une description de slot a la forme (slot-name slot-option *), où chaque option est un mot-clé suivi d'un nom, d'une expression et d'autres options. Les options de slot les plus couramment utilisées sont -

  • :accessor nom-fonction

  • :initform expression

  • :initarg symbole

Par exemple, définissons une classe Box, avec trois emplacements de longueur, largeur et hauteur.

(defclass Box () 
   (length 
   breadth 
   height)
)

Fourniture d'un contrôle d'accès et de lecture / écriture à un emplacement

À moins que les emplacements n'aient des valeurs accessibles, lues ou écrites, les classes sont pratiquement inutiles.

Vous pouvez spécifier accessorspour chaque emplacement lorsque vous définissez une classe. Par exemple, prenez notre classe Box -

(defclass Box ()
   ((length :accessor length)
      (breadth :accessor breadth)
      (height :accessor height)
   )
)

Vous pouvez également spécifier des accessor noms pour lire et écrire un slot.

(defclass Box ()
   ((length :reader get-length :writer set-length)
      (breadth :reader get-breadth :writer set-breadth)
      (height :reader get-height :writer set-height)
   )
)

Créer une instance d'une classe

La fonction générique make-instance crée et retourne une nouvelle instance d'une classe.

Il a la syntaxe suivante -

(make-instance class {initarg value}*)

Exemple

Créons une classe Box, avec trois emplacements, longueur, largeur et hauteur. Nous utiliserons trois accesseurs d'emplacement pour définir les valeurs dans ces champs.

Créez un nouveau fichier de code source nommé main.lisp et tapez le code suivant dedans.

(defclass box ()
   ((length :accessor box-length)
      (breadth :accessor box-breadth)
      (height :accessor box-height)
   )
)
(setf item (make-instance 'box))
(setf (box-length item) 10)
(setf (box-breadth item) 10)
(setf (box-height item) 5)
(format t "Length of the Box is ~d~%" (box-length item))
(format t "Breadth of the Box is ~d~%" (box-breadth item))
(format t "Height of the Box is ~d~%" (box-height item))

Lorsque vous exécutez le code, il renvoie le résultat suivant -

Length of the Box is 10
Breadth of the Box is 10
Height of the Box is 5

Définition d'une méthode de classe

le defmethodmacro vous permet de définir une méthode à l'intérieur de la classe. L'exemple suivant étend notre classe Box pour inclure une méthode nommée volume.

Créez un nouveau fichier de code source nommé main.lisp et tapez le code suivant dedans.

(defclass box ()
   ((length :accessor box-length)
      (breadth :accessor box-breadth)
      (height :accessor box-height)
      (volume :reader volume)
   )
)

; method calculating volume   

(defmethod volume ((object box))
   (* (box-length object) (box-breadth object)(box-height object))
)

 ;setting the values 

(setf item (make-instance 'box))
(setf (box-length item) 10)
(setf (box-breadth item) 10)
(setf (box-height item) 5)

; displaying values

(format t "Length of the Box is ~d~%" (box-length item))
(format t "Breadth of the Box is ~d~%" (box-breadth item))
(format t "Height of the Box is ~d~%" (box-height item))
(format t "Volume of the Box is ~d~%" (volume item))

Lorsque vous exécutez le code, il renvoie le résultat suivant -

Length of the Box is 10
Breadth of the Box is 10
Height of the Box is 5
Volume of the Box is 500

Héritage

LISP vous permet de définir un objet en termes d'un autre objet. C'est appeléinheritance.Vous pouvez créer une classe dérivée en ajoutant des fonctionnalités nouvelles ou différentes. La classe dérivée hérite des fonctionnalités de la classe parent.

L'exemple suivant explique cela -

Exemple

Créez un nouveau fichier de code source nommé main.lisp et tapez le code suivant dedans.

(defclass box ()
   ((length :accessor box-length)
      (breadth :accessor box-breadth)
      (height :accessor box-height)
      (volume :reader volume)
   )
)

; method calculating volume   
(defmethod volume ((object box))
   (* (box-length object) (box-breadth object)(box-height object))
)
  
;wooden-box class inherits the box class  
(defclass wooden-box (box)
((price :accessor box-price)))

;setting the values 
(setf item (make-instance 'wooden-box))
(setf (box-length item) 10)
(setf (box-breadth item) 10)
(setf (box-height item) 5)
(setf (box-price item) 1000)

; displaying values
(format t "Length of the Wooden Box is ~d~%" (box-length item))
(format t "Breadth of the Wooden Box is ~d~%" (box-breadth item))
(format t "Height of the Wooden Box is ~d~%" (box-height item))
(format t "Volume of the Wooden Box is ~d~%" (volume item))
(format t "Price of the Wooden Box is ~d~%" (box-price item))

Lorsque vous exécutez le code, il renvoie le résultat suivant -

Length of the Wooden Box is 10
Breadth of the Wooden Box is 10
Height of the Wooden Box is 5
Volume of the Wooden Box is 500
Price of the Wooden Box is 1000