1 / 15

Using Classes

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* ).

duyen
Download Presentation

Using Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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* )

  2. 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.

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

  4. 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.

  5. Creating objects • To create an instance of Bus, you use the make-instancefunction 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.

  6. 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))

  7. 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))

  8. 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)

  9. 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 …

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

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

  12. :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) ) )

  13. :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))

  14. Shared slot for instances of a class (1) • You can use the keyword :allocationto 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)) )

  15. 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)

More Related