Using Classes Lisp also supports the Object-Oriented paradigm. The process of defining classes in Lisp is similar to how you you define structures. The.

Slides:



Advertisements
Similar presentations
24-Aug-14 Abstract Classes and Interfaces. Java is “safer” than Python Python is very dynamic—classes and methods can be added, modified, and deleted.
Advertisements

HTML Overview - Cascading Style Sheets (CSS). Before We Begin Make a copy of one of your HTML file you have previously created Make a copy of one of your.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Object-Oriented Programming In Lisp. Lisp vs. Scheme 1.Lisp has much more built-in functions and special forms, the Scheme language definition takes 45.
CLOS To start off with, CLOS (pronounced “kloss” or “see- loss”) is very different from other object oriented programming languages –Multiple inheritance.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Arrays (1) You create an array in LISP by using the function (make- array ). All elements are initially set to nil. To create a 1-dimensional array of.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
Structures Structures in LISP are analogous to that in C. To create a structure, use the following syntax: (defstruct Name Slot1 Slot2 Slot3) An example.
By: Dr. Christel Kemke Department of Computer Science University of Manitoba.
Prof. Fateman CS164 Lecture 251 Languages as a Tool for Software Engineering Piercing the Mysteries of Object-Oriented Programming Lecture 25.
Chapter 6 Class Hierarchies, Inheritance, and Interfaces  This is a very good chapter for us at this point in time.  We have been programming hard this.
CS 106 Introduction to Computer Science I 03 / 19 / 2008 Instructor: Michael Eckmann.
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Common Lisp Object System (CLOS)
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
C++ fundamentals.
F RANZ I NC. An In-Depth Look at Simple Streams By Duane Rettig October, 2002.
1 LISP (Lecture Note #7) 인공지능 이복주 단국대학교 컴퓨터공학과. 2 Slide made by Bogju Lee Outline  What is LISP  Variables and Functions  Data Types  SETF  Math.
Def D OC An extensible, dynamic document creation system By Rahul Jain
© 2006 Pearson EducationInterfaces1 of 28 INTERFACES Declaring Interfaces Implementing Interfaces Using Interfaces Polymorphically Visibility Modifiers.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
1 CLOS: Common Lisp Object System Basic principles of CLOS Classes, Instances and Slots Inheritance Generic functions Methods Multi-methods Comparison.
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
1/38 Andries van Dam  /29/15 Pet-able! Lecture 6 Interfaces.
ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
Review of objects  overview overview. Class of objects  Attributes/ methods.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Object Oriented Programming COP3330 / CGS5409.  Inheritance  Assignment 5.
Intelligence Musical and Otherwise.  Huh? Huh? Webster’s definition  Intelligence:  The ability to reason.
Lec 14 Writing an Instantiable Class III. Agenda Review of Instantiable Classes Scope of variables Using this to override scope issues Lab: Creating a.
OOP with Objective-C Categories, Protocols and Declared Properties.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Introduction to Object-Oriented Programming Lesson 2.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
1 Software Engineering Lecture 15 Object Oriented Software Design in Java.
Structures “The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Classes, Interfaces and Packages
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
CIS162AD Constructors Part 2 08_constructors.ppt.
Object-Oriented Programming: Inheritance and Polymorphism.
OOP Basics Classes & Methods (c) IDMS/SQL News
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Objects and Classes Gul Agha CS 421 Spring /11/2001CS 322 Fall Characteristics of OOP Object-based  encapsulate state  provide interface.
A Introduction to Computing II Lecture 3: Interfaces and Inheritance Fall Session 2000.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
PaintPictureBoxDemo Refers to the PaintPictureBoxDemo Visual Basic Program Included With The Lecture.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Prof. Fateman CS164 Lecture 35
Teaching a machine a simple taxonomy
Objects as a programming concept
Allegro CL Certification Program
CLOS CSC 358/
PHP Classes and Objects
Chapter 11: Classes, Instances, and Message-Handlers
topics object oriented design objects and classes properties
Chapter 14 Abstract Classes and Interfaces
Notes from Week 5 CSE 115 Spring 2006 February 13, 15 & 17, 2006.
CMSC202 Computer Science II for Majors Lecture 10 and 11 – Inheritance
Presentation transcript:

Using Classes Lisp also supports the Object-Oriented paradigm. The process of defining classes in Lisp is similar to how you you define structures. The syntax of defining classes are as follows: (defclass Class_name (Superclass*) (Slot_definition*) Class_option* )

Slot definition Slots are properties belonging to the class. Slots are lists containing: –Slot name, optionally followed by –Slot option keyword and associated value You can provide an initial/default value of a slot, or you can set the value of a slot in your program during run-time. Lisp allows you the flexibility of specifying how you would want to access slot values.

A simple class Here is a simple class defined in Lisp. (defclass Bus (Vehicle) ( (Make) (Color) (Year) ) (:documentation “The bus class”) )

Class Vehicle (defclass Vehicle () () ) This is the superclass for the Bus class. You would have to define this superclass first before defining Bus. Vehicle is an empty class. It specifies no slots.

Creating objects To create an instance of Bus, you use the make- instance function as follows: (setq bus1 (make-instance ‘Bus)) Recall that Bus has 3 slots, namely make, color and year. Now let’s look at some keywords that are used to initialize, read and write the values of these slots.

Initialize Slot Value To provide an initial value for a slot, use :initform (defclass Bus (Vehicle) ( (Make :initform ‘Ford) (Color :initform ‘Blue) (Year :initform 1989) ) (setq bus1 (make-instance ‘Bus))

Reading/Writing slot values (1) To read slot values, you can use the keyword :reader, which specifies the name of the function that will read the slot, while :writer specifies the name of the function to write to the slots. Continuing with the previous example: (defclass Bus (Vehicle) ( (Make :reader make :writer Set-Make) (Color :reader color :writer Set-Color) (Year :reader year :writer Set-Year) ) (setq bus1 (make-instance ‘Bus))

Reading/Writing slot values (2) Now let’s say that, we want to write values to the slots Color, Year and Make, the code below will perform this: (Set-Color ‘yellow bus1) (Set-Make ‘Ford bus1) (Set-Year 1989 bus1) To read the value of those slots: (Color bus1) (Make bus1) (Year bus1)

Using :accessor (1) For each slot, specifying one function for reading its value and another function to write its value will be inefficient if there are a lot of slots. It is better to just specify ONE function that is able to perform both read and write for a slot. You can do this using the :accessor keyword. Going back to our running example …

Using :accessor (2) (defclass Bus () ( (Make :accessor make) (Color :accessor color) (Year :accessor year) ) (setq bus1 (make-instance ‘Bus)) To set the slot Year to 2000, do this: (setf (year bus1) 2000) Notice that setf is used to assign the desired value to the slot.

Using :accessor (3) To read the value of the slot Year, do the following: (year bus1) Try this for the remaining slots.

:initarg (1) This is a way to pass in any initialization values for the slots during make-instance. The value specified using :initarg will override the value specified using :initform (defclass Bus () ( (Make :accessor make) (Color :accessor color) (Year :accessor year :initarg :year :initform 1989) )

:initarg (2) To pass in an initial value of year while creating an instance of Cars, do this: (setq bus1 (make-instance ‘Bus :Year 2000))

Shared slot for instances of a class (1) You can use the keyword :allocation to specify whether a slot’s value is specific to each instance of the class, or the slot’s value is shared among all instances of the class. Now let’s create a subclass for our Bus as follows: (defclass SchoolBus (Bus) ((MaxSpeed :accessor MaxSpeed :initform 320 :allocation :class)) )

Shared slot for instances of a class (2) (setq schoolbus1 (make-instance ‘SchoolBus)) (setq schoolbus2 (make-instance ‘SchoolBus)) (MaxSpeed schoolbus1) (MaxSpeed schoolbus2) (setf (MaxSpeed schoolbus1) 1000) (MaxSpeed schoolbus1) (MaxSpeed schoolbus2)