CLOS CSC 358/458 6.5.2006.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Types in Ruby and other languages….  Classes and objects (vs prototypes)  Instance variables/encapsulation  Object creation  Object equality/comparison.
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Generics "It is easy to study the rules of overloading and of templates without noticing that together they are one of the keys to elegant and efficient.
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.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
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.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Common Lisp Object System (CLOS)
Chapter 10 Classes Continued
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
1 CLOS: Common Lisp Object System Basic principles of CLOS Classes, Instances and Slots Inheritance Generic functions Methods Multi-methods Comparison.
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
Object Oriented Programming
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
The Object-Oriented Thought Process Chapter 03
Object-Oriented Programming
Polymorphism in Methods
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
JAVA By Waqas.
Allegro CL Certification Program
Inheritance and Polymorphism
FUNCTIONS In C++.
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Inheritance in Java.
Object Oriented Programming in Java
Types of Programming Languages
Section 11.1 Class Variables and Methods
Programming Language Concepts (CIS 635)
Object Oriented Programming
Understanding Inheritance
CSC 143 Inheritance.
Object Oriented Programming (OOP) LAB # 8
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Week 6 Object-Oriented Programming (2): Polymorphism
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Fundaments of Game Design
COP 3330 Object-oriented Programming in C++
Chapter 14 Abstract Classes and Interfaces
Java Programming Language
Chapter 8 Class Inheritance and Interfaces
Final and Abstract Classes
Inheritance Lakshmish Ramaswamy.
Presentation transcript:

CLOS CSC 358/458 6.5.2006

Outline CLOS Basic features OO programming Examples

CLOS Common Lisp Object System Adds object-oriented programming to Lisp OOP is a choice like C++

CLOS features Built-in classes integrated with type system User-defined classed also part of type system Multiple inheritance Class variables (static) and instance variables Programmer-selected method combination Multiple dispatch

defclass creates a new class supply name superclasses slot specifiers class options

Example (defclass boat () (capacity)) (defclass submarine (boat) (speed depth))

make-instance Creates an instance of a class Example (make-instance 'submarine)

slot-value Allows access to defined slot Example instance variables (setf s1 (make-instance 'submarine)) (setf (slot-value s1 'speed) 100)

defmethod Methods are defined separately from classes Methods are specialized by the class they operate on

Example (setf (slot-value sub 'depth) new-depth)) (defmethod dive ((sub submarine) new-depth) (setf (slot-value sub 'depth) new-depth))

Messaging Model of OOP Most OO languages assume a message passing model The method signature = message Object with the method = recipient of message But what about double-dispatch transfer (account1, account2) or even equals (obj1, obj2) We are forced to pick one object implement method there

Dispatch Model of OOP Methods independent of objects Methods specialized for objects but can be specialized for multiple objects

Example For numeric types, add can be multiply defined In Java or C++, add (integer integer) add (integer float) add (float float) add (float double) ... etc. In Java or C++, a single (base) class must be privileged

Multiple inheritance Multiple inheritance is handled by listing the superclasses (defclass tarot-card (playing-card fortune-card) ...) Standard problem how to inherit methods especially if the superclasses share an ancestor Lisp finds all applicable methods sorts in precedence order specific to general left to right (although the programmer can control this, too!) applies as many as necessary Standard multiple inheritance problem doesn't appear because there can be only one method with a given dispatch signature

Slot specification slot-value is awkward can't be compiled defclass has slot specifications :accessor :reader :writer :initform :initarg :allocation :type

initform Default initial value Can be overridden by arguments to make-instance Evaluated each time make-instance is called

initarg A keyword that can be passed to make-instance Gives slot its initial value Overrides initform

Example (defclass Submarine () ((Depth :initarg :depth :initform 100) (Speed :reader Speed :initform 5))) (setq Sub-1 (make-instance 'Submarine :depth 200))

Accessors reader writer accessor Name of a function that will read the slot writer Name of a function that will set the slot's value accessor Name of a function to read and (with setf) write the slot's value

Example 1 (defclass Submarine () ((Depth :reader Depth :writer Set-Depth :initform 100) (Speed :reader Speed :writer Set-Speed :initform 5))) (setq Sub-1 (make-instance 'Submarine)) (Depth Sub-1) ==> 100 (Speed Sub-1) ==> 5 (Set-Depth 200 Sub-1) (Set-Speed 4 Sub-1) (Depth Sub-1) ==> 200 (Speed Sub-1) ==> 4

Example 2 (defclass Submarine () ((Depth :accessor Depth :initform 100) (Speed :accessor Speed :initform 5))) (setq Sub-1 (make-instance 'Submarine)) (Depth Sub-1) ==> 100 (Speed Sub-1) ==> 5 (setf (Depth Sub-1) 200) (setf (Speed Sub-1) 4) (Depth Sub-1) ==> 200 (Speed Sub-1) ==> 4

type Specifies the type of the argument Can be used for optimization

allocation Specifies instance or class allocation Instance allocation means new copy of slot for each instance instance variable in Java Class allocation means one copy of instance for all instances class variable in Java declared with "static" (yuk!)

Other Class Options documentation default-initargs doc string associated with class default-initargs useful for overriding initforms from superclass

Example A class for cards A subclass for tarot cards

defgeneric Sort of like interface definition Specify method name and argument list Also method-combination argument-precedence-order

Method combination How to inherit methods? normally evaluate most-specific method polymorphism! But there are other possibilities Discounts Supplier discount = 10% Preferred supplier discount = an additional 2% We might want to specify that discounts are combined rather than overridden in Java, must call super.method directly

In CLOS (defgeneric get-discount (firm) (:method-combination +)) (defclass supplier () ((discount :initform 10))) (defmethod get-discount + ((firm supplier)) (slot-value firm 'discount)) (defclass preferred-supplier (supplier) ()) (defmethod get-discount + ((firm preferred-supplier)) 2)

Method Combination Specify type of method combination in generic function Each method is marked with its combination method Methods are called from least to most-specific Combination methods progn, +, list, and, or, max, min, append and nconc

Example Building a deck Combine methods with append standard-deck tarot-deck has a deck built the same way plus additional ones Combine methods with append

Example Alternative :around method call-next-method

Example Implementing gin with objects