Top Banner
CS61A Lecture 14 2011-07-13 Colleen Lewis
36

CS61A Lecture 14

Jan 21, 2016

Download

Documents

CS61A Lecture 14. 2011-07-13 Colleen Lewis. Clicker poll . Do you work with another person on the homework? Yes Sometimes No. Object Oriented Programming (OOP) Overview. Vocab & Scheme keywords. Class – like a blueprint of an object define-class - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS61A Lecture 14

CS61A Lecture 14

2011-07-13Colleen Lewis

Page 2: CS61A Lecture 14

Clicker poll Do you work with another person on the

homework? A)YesB)SometimesC)No

Page 3: CS61A Lecture 14

Object Oriented Programming (OOP)

OverviewMultiple independent intelligent agents

Message passing, local state, inheritance

define-class, instantiate, ask, method, instance-vars, class-

vars, self, usual, parent

????

Page 4: CS61A Lecture 14

Vocab & Scheme keywords

• Class – like a blueprint of an object– define-class

• Instance of a class – a particular object– instantiate

• Method – something you can ask an instance of a class to do. – method– ask

Page 5: CS61A Lecture 14

Methods

Page 6: CS61A Lecture 14

The doubler class

(define-class (doubler)

(method (say stuff)

(se stuff stuff)))

Class name

Method name Method

body

Method argument variable

Page 7: CS61A Lecture 14

Creating objects & calling methods

STk> (define d (instantiate doubler))

d

STk> (ask d 'say '(how are you?))

(how are you? how are you?)

Class name

Creates an instance of

a classCall a

method

On this instance of

a classCall this method

With this argument

Page 8: CS61A Lecture 14

Modify the doubler class(define-class (doubler)

(method (say stuff)

(se stuff stuff)))

STk> (ask d 'add 2 3)

10

STk> (ask d 'add 1 1)

4

‘add is a: A) function B) method C) class D)message

Page 9: CS61A Lecture 14

SOLUTION Modify the doubler class

(define-class (doubler)

(method (say stuff)

(se stuff stuff))

(method (add num1 num2)

(* 2 (+ num1 num2))))

Method name Method

arguments

Method body

Page 10: CS61A Lecture 14

instance variables

instance-vars

Page 11: CS61A Lecture 14

Vocab

• Instance variables – variables local to an instance of a class– instance-vars

Page 12: CS61A Lecture 14

instance-vars(define-class (counter)

(instance-vars (count 0) )

(method (welcome)

(se 'my 'count 'is count)))

Create these variables for each

new instance

Instance variable name Initial value

Could add another variable here. E.g.

(x 3)

Can be accessed

Page 13: CS61A Lecture 14

When do you use quotes?

(define-class (counter)

(instance-vars (count 0))

(method (welcome)

(se 'my 'count 'is count)))STk> (define c (instantiate counter))

c

STk> (ask c 'welcome)

(my count is 0)

Which needs a quote? A) Class name B) method name C) both D) neither

?

?

Page 14: CS61A Lecture 14

If you change the class,ALWAYS recreate your objects

STk> (load "lect14.scm")

okaySTk> (define c (instantiate counter))

c

STk> (ask c 'welcome)

(my count is 0)

Page 15: CS61A Lecture 14

Accessing instance variables(define-class (counter)

(instance-vars (count 0) (x 3))

(method (welcome)

(se 'my 'count 'is count)))

STk> (define c (instantiate counter))

c

STk> (ask c 'count)

0

STk> (ask c 'x)

3

Methods for instance variables are

provided automatically

Page 16: CS61A Lecture 14

set!

Non-functional programming(A way to change instance variables)

Page 17: CS61A Lecture 14

Changing instance variablesSTk> (define c (instantiate counter))

cSTk> (ask c 'count)

0

STk> (ask c 'next)

1

STk> (ask c 'next)

2

STk> (ask c 'count)

2

Page 18: CS61A Lecture 14

Changing instance variables(define-class (counter)

(instance-vars (count 0))

(method (next)

(set! count (+ count 1))

count))Non-functional programming so you may do many things

in one method. Scheme returns the last one

New value

Variable to change

Page 19: CS61A Lecture 14

Add a method addX(define-class (counter)

(instance-vars (count 0) (x 0))

(method (next)

(set! count (+ count 1))

count))

STk>(ask c 'next)

1

STk> (ask c 'addX 20)

21

STk> (ask c 'x)

20

What was the argument name in

your addX method?

A)xB)argXC)yD)None used

Page 20: CS61A Lecture 14

Solution addX

(define-class (counter)

(instance-vars (count 0) (x 0))

(method (addX argX)

(set! count (+ count argX))

(set! x argX)

count)) I don’t want the argument to be named x b/c then I would need to

write (set! x x)

Page 21: CS61A Lecture 14

Concept: Local State

Page 22: CS61A Lecture 14

STk> (define c1 (instantiate counter))

c1

STk> (define c2 (instantiate counter))

c2

STk> (ask c1 'next)

1

STk> (ask c1 'next)

2

STk> (ask c2 'count)

0

STk> (ask c2 'next)

1

STk> (ask c1 'count)

2

c2’s count wasn’t changed

Page 23: CS61A Lecture 14

Class variables

Uses the keyword class-vars

Page 24: CS61A Lecture 14

Vocab

• Instance variables – variables local to an instance of a class– instance-vars

• Class variables – variables shared by all instances of a class– class-vars

Page 25: CS61A Lecture 14

STk> (define c1 (instantiate counter))

c1

STk> (define c2 (instantiate counter))

c2

STk> (ask c1 'next)

(count: 1 total: 1)

STk> (ask c1 'next)

(count: 2 total: 2)

STk> (ask c1 'next)

(count: 3 total: 3)

STk> (ask c2 'next)

A(count: 1 total: 4) B(count: 1 total: 1)

C(count: 4 total: 4) D(count: 4 total: 1)

total is a class variable shared by all instances of

the class

What will this print?

Page 26: CS61A Lecture 14

Class variables in Scheme OOP

(define-class (counter)

(instance-vars (count 0))

(class-vars (total 0))

(method (next)

(set! count (+ count 1))

(set! total (+ total 1))

(se 'count: count

'total: total)))

Counter objects

respond to the

message 'total

Page 27: CS61A Lecture 14

Instantiation Variables

Page 28: CS61A Lecture 14

Vocab

• Instance variables – variables local to an instance of a class– instance-vars

• Instance of a class – a particular object– instantiate

• Instantiation variables – arguments provided when we created the instance of the class.

Page 29: CS61A Lecture 14

(define-class (beach-bum name)

(instance-vars (surfs #t)))

STk> (define surfer (instantiate beach-bum 'bob))

surfer

STk> (ask surfer 'name)

bob

STk> (ask surfer 'surfs)

#t

Instantiation variable

Instance variable

Created differently but they work the

same way

Page 30: CS61A Lecture 14

Write the meet methodSTk> (load "lect14.scm")

okaySTk> (define surfer (instantiate beach-bum 'bob))

surfer

STk> (ask surfer 'meet 'cs61a-class)

(hi cs61a-class my name is bob dude)

'cs61a-class is the value of an A) instance variable B) instantiation variable C) method argument

Page 31: CS61A Lecture 14

meet solution

(define-class (beach-bum name)

(instance-vars (surfs #t))

(method (meet someone)

(se 'hi someone

'my 'name 'is name 'dude)))

Page 32: CS61A Lecture 14

The initialization keyword

A way to initialize class variables.

Page 33: CS61A Lecture 14

surfer-names is….STk> (define s1 (instantiate beach-bum 'bob))

s1

STk> (ask s1 'surfer-names)

(bob)STk> (define s2 (instantiate beach-bum 'jim))

s2

STk> (ask s1 'surfer-names)

(jim bob)

A)An instance variable B) An instantiation variableC) A class variable D) Something else

Page 34: CS61A Lecture 14

Initializing class-vars

(define-class (beach-bum name)

(class-vars (surfer-names '()))

(initialize (set! surfer-names (se name surfer-names)))

(method (say stuff)

(se stuff 'dude)))

We already knew how to make class

variables

This is the FIRST initial value

If other instances of the class already

exist, do this

Page 35: CS61A Lecture 14

Vocab• Class

– like a blueprint of an object– define-class

• Instance of a class – a particular object– instantiate

• Method – something you can ask an instance of a class to do. – method– ask

Page 36: CS61A Lecture 14

Vocab • Instance variables – variables local to an instance of a class– instance-vars

• Instance of a class – a particular object– instantiate

• Instantiation variables – arguments provided when we created the instance of the

class.

• Class variables– variables shared by all instances of a class– class-vars