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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read