cs61a lecture 14
DESCRIPTION
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 PresentationTRANSCRIPT
![Page 1: CS61A Lecture 14](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/1.jpg)
CS61A Lecture 14
2011-07-13Colleen Lewis
![Page 2: CS61A Lecture 14](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/2.jpg)
Clicker poll Do you work with another person on the
homework? A)YesB)SometimesC)No
![Page 3: CS61A Lecture 14](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/5.jpg)
Methods
![Page 6: CS61A Lecture 14](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/10.jpg)
instance variables
instance-vars
![Page 11: CS61A Lecture 14](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/11.jpg)
Vocab
• Instance variables – variables local to an instance of a class– instance-vars
![Page 12: CS61A Lecture 14](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/16.jpg)
set!
Non-functional programming(A way to change instance variables)
![Page 17: CS61A Lecture 14](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/21.jpg)
Concept: Local State
![Page 22: CS61A Lecture 14](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/23.jpg)
Class variables
Uses the keyword class-vars
![Page 24: CS61A Lecture 14](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/26.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/27.jpg)
Instantiation Variables
![Page 28: CS61A Lecture 14](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/28.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/29.jpg)
(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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/30.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/31.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/32.jpg)
The initialization keyword
A way to initialize class variables.
![Page 33: CS61A Lecture 14](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/33.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/34.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/35.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022051001/56814fbb550346895dbd73ee/html5/thumbnails/36.jpg)
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