Common LISP Object System
Last Updated :
22 Aug, 2022
CLOS or Common LISP Object System is one of the most powerful object systems available. It is dynamic, reliant, and supports multiple inheritance and multiple dispatches. It is different from most object systems as it has classes and functions(methods) that are not tied together. Also, it is provided by a protocol known as a meta-object protocol, which offers a standard interface for the CLOS, and can be used to create new object systems
Defining Classes:
For defining classes in Lisp we use defclass macro.
Syntax:
(defclass <class-name> (list of super classes)
((slot-1
:slot-option slot-argument)
(slot-2, etc))
(:optional-class-option
:another-optional-class-option))
(defclass Name ()
(First_Name
Middle_Name
Last_Name)
)
; This code makes a class 'Name' with 3 slots
The slots are the variables that stores data and fields in them. The slots come with multiple slot options comprising of keywords and name, expression, and other options which are:
- :accessor function-name
- :initform expression
- :initarg symbol
Creating Instances of Class:
Instances could be easily made using make-instance.
Syntax:
make-instance class {initarg value}*)
Example 1:
Lisp
; Defining a class Names with
; Slots First_Name and Last_Name
(defclass Names ()
((First_Name :accessor name_1)
(Last_Name :accessor name_2)
)
)
; Creating a instance
(setf Person (make-instance 'Names))
; Setting the name for the instance
(setf (name_1 Person) "Kishan")
(setf (name_2 Person) "Pandey")
; Printing the name
(format t "The First Name of the
person is ~d~%" (name_1 Person))
(format t "The Last Name of the
person is ~d~%" (name_2 Person))
Output:
Providing Access and Read/Write Control to a Slot:
For making a functional class we have functionalities to access, read and write into the slot. Specifying accessors for each slot while defining the class:
(defclass Names ()
((First_Name :accessor name_1)
(Last_Name :accessor name_2)
)
)
Note: You can also specify separate accessor names for reading and writing a slot
Lisp
(defclass Names ()
((First_Name :reader get-First_Name :writer set-First_Name)
(Last_Name :reader get-Last_Name :writer set-Last_Name)
)
)
Inheritance :
Inheritance is a method to define one object(child-object) in terms of another(parent-object). In Lisp, we can define child classes that inherit the properties of the parent class.
Example 2:
Lisp
(defclass BMI ()
(
(weight :accessor person-weight)
(height :accessor person-height)
(BMIcalc :reader BMI-calc)
)
)
; method calculating BMI
(defmethod BMIcalc ((object BMI))
(/(person-weight object)(*(person-height object)(person-height object)))
)
;child_BMI inherits BMI class
(defclass child-BMI (BMI)
((health :accessor person-health)))
;setting the values
(setf item (make-instance 'child-BMI))
(setf (person-weight item) 70)
(setf (person-height item) 1.8)
(setf (person-health item) "Good")
; displaying the values
(format t "BMI is: ~d~%" (BMIcalc item))
(format t "Health is: ~d~%" (person-health item))
Output:
Defining the Method of a Class:
We will use the defmethod macro to define a method inside the class as follows:
Example 3:
Lisp
(defclass BMI ()
(
(weight :accessor person-weight)
(height :accessor person-height)
(BMIcalc :reader BMI-calc)
)
)
; Defining a method of class that calculates the BMI
(defmethod BMIcalc ((object BMI))
(/(person-weight object)(*(person-height
object)(person-height object)))
)
;setting the values
(setf item (make-instance 'BMI))
(setf (person-weight item) 70)
(setf (person-height item) 1.8)
; displaying values
(format t "BMI is: ~d~%" (BMIcalc item))
Output:
Similar Reads
Distributed Object Systems Distributed Object Systems (DOS) enable the interaction of objects across different networked locations, allowing software components to communicate seamlessly. This architecture supports a wide range of applications, from enterprise systems to cloud computing. In this article, we will explore the f
6 min read
Distributed Computing System Models Distributed computing is a system where processing and data storage is distributed across multiple devices or systems, rather than handled by a single central device. In this article, we will see Distributed Computing System Models. Important Topics for Distributed Computing System Models Types of D
8 min read
Multi-User Operating System An operating system is software that acts as an interface between the user and the computer hardware which does multiple functions such as memory management; file management and processor management. The operating system should have to meet the requirements of all its users in a balanced way so that
5 min read
Object-Oriented Design (OOD) - System Design A crucial method for system design is object-oriented design (OOD), which places an intense focus on scalability, modularity, and reusability. OOD resembles real-world systems by encapsulating data and behavior into objects, which facilitates understanding programs and maintenance. By utilizing conc
12 min read
How Nodes Communicate in Distributed Systems? In distributed systems, nodes communicate by sending messages, invoking remote procedures, sharing memory, or using sockets. These methods allow nodes to exchange data and coordinate actions, enabling effective collaboration towards common goals. Important Topics to Understand Communication Between
10 min read