DOC PREVIEW
Berkeley COMPSCI 61A - OBJECT ORIENTED PROGRAMMING

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

IntroductionMessage PassingLocal StateInheritanceOBJECT ORIENTED PROGRAMMING 11GEORGE [email protected] of Electrical Engineering and Computer SciencesUniversity of California, BerkeleyJuly 12, 20101 IntroductionUp until now, we’ve been dealing with functional programming, where there’s no changing of data. We’vealways been just concerned with the input/output pairing of a function. For the next week and a half, we’llbe changing that viewpoint.This entire week is about object oriented programming. Today and tomorrow, we’ll discuss an Object Ori-ented framework. Later this week, we’ll examine how this framework is actually implemented in Scheme.What we’ll now be thinking of, instead of a kind of mathematical view of computing, is we’ll be movingto a view of having many agents acting more or less independently inside a computer. In Functional Pro-gramming, we’ve dealt with one large program created by composing various smaller programs together.In this next view, a view of Object Oriented Programming, the idea is that there are multiple things, each ofwhich is designed to specialize at a certain task.To have this happen, we need three key things:• Message Passing - Enables cooperation and collaboration between objects.• Local State - An object can remember things about its past history• Inheritance - Enables specialization so that objects can fulfill niche roles without sacrificing the biggerpictures.2 Message PassingThe first, message passing, is something we’ve already seen last week. As a reminder, message passingis the paradigm where instead of having functions that do things, our data is capable of handling theoperations we need. Instead of operating on data, we tell the data what to do, and it is able to do it on itsown.1Let’s look at a program to do the same message passing things we did last week:;;;;; In file cs61a/lectures/3.0/demo.scm(define-class (square side)(method (area)(*side side))(method (perimeter)(*4 side)) )> (define sq (instantiate square 5))> (ask sq ’area)25> (ask sq ’perimeter)120Let’s look at the things we just introduced. First, we should think about classes, instances, and methods.A class is a blueprint for something. We create a class using DEFINE-CLASS in Scheme. It tells the computerwhat to do when we have a specific instance of it. However, note that we cannot manipulate a class directly,since it doesn’t exist! We have to create an instance of it to do something. For example, I have the idea of adog. But, I can’t pet it, even know that I know that dogs can be petted.An instance or an object is something that actually exists. To create an object, we use INSTANTIATE in Scheme.If a class a blueprint, then an instance is the physical building. This has specific properties and things thatit does. With an instance, we can actually do things to them and on them. For example, we can pet aninstance of a dog.An INSTANTIATION VARIABLE is something that is given to a class on instantiation. My canonical examplefor this is your name. When you were born, you were given a name. This is what an instantiation variableis.A METHOD is a message that the class understands. This message can take in arguments much in the sameway that procedures can, which we’ll see in a bit. We call a method using ASK.Now, I’ve basically explained everything about this program. However, besides funny syntax, there’s noth-ing about this program that distinguishes it from anything you did last week. There’s nothing beyondmessage passing in this program.3 Local StateLet’s change that:;;;;; In file cs61a/lectures/3.0/demo.scm(define-class (counter)(instance-vars (count 0))(method (next)(set! count (+ count 1))count) )> (define c1 (instantiate counter))> (ask c1 ’next)1> (ask c1 ’next)21Oops. This said something else in the draft handed out in lecture.2> (define c2 (instantiate counter))> (ask c2 ’next)1> (ask c1 ’next)3Each counter has its own instance variable to remember how many times it’s been sent the next message.Don’t get confused about the terms instance variable versus instantiation variable. They are similar in thateach instance has its own version; the difference is that instantiation variables are given values when aninstance is created, using extra arguments to instantiate, whereas the initial values of instance variables arespecified in the class definition and are generally the same for every instance (although the values maychange as the computation goes on.) For example, your name might be an instantiation variable, in thesense that it’s assigned at birth. However, INSTANCE-VARS are things like weight and height (if we think allbabies are the same weight and height).Let’s look at a method with an argument:;;;;; In file cs61a/lectures/3.0/demo.scm(define-class (doubler)(method (say stuff) (se stuff stuff)))> (define dd (instantiate doubler))> (ask dd ’say ’hello)(hello hello)> (ask dd ’say ’(she said))(she said she said)We can also define variables that are not merely shared across instances, but shared across classes:;;;;; In file cs61a/lectures/3.0/demo1.scm(define-class (counter)(instance-vars (count 0))(class-vars (total 0))(method (next)(set! total (+ total 1))(set! count (+ count 1))(list count total)))> (define c1 (instantiate counter))> (ask c1 ’next)(1 1)> (ask c1 ’next)(2 2)> (define c2 (instantiate counter))> (ask c2 ’next)(1 3)> (ask c1 ’next)(3 4)4 InheritanceNow, we’ve pretty much covered the first two parts, let’s look at the last element. To understand the ideaof inheritance, we’ll first define a person class that knows about talking in various ways, and then definea pigger class that’s just like a person except for talking in Pig Latin.3;;;;; In file cs61a/lectures/3.0/demo1.scm(define-class (person name)(method (say stuff) stuff)(method (ask stuff)(ask self ’say (se ’(would you please) stuff)))(method (greet)(ask self ’say (se ’(hello my name is) name))) )> (define marc (instantiate person ’marc))> (ask marc ’say ’(good morning))(good morning)> (ask marc ’ask ’(open the door))(would you please open the door)> (ask marc ’greet)(hello my name is marc)Notice that an object can refer to itself by the name self; this is an automatically-created instance variablein every object whose value is the object itself. (We’ll see when we look below the line that there are somecomplications about making this work.);;;;; In file


View Full Document

Berkeley COMPSCI 61A - OBJECT ORIENTED PROGRAMMING

Documents in this Course
Lecture 1

Lecture 1

68 pages

Midterm

Midterm

5 pages

Midterm

Midterm

6 pages

Lecture 35

Lecture 35

250 pages

Lecture 14

Lecture 14

125 pages

Lecture 2

Lecture 2

159 pages

Lecture 6

Lecture 6

113 pages

Lecture 3

Lecture 3

162 pages

Homework

Homework

25 pages

Lecture 13

Lecture 13

117 pages

Lecture 29

Lecture 29

104 pages

Lecture 11

Lecture 11

173 pages

Lecture 7

Lecture 7

104 pages

Midterm

Midterm

6 pages

Midterm

Midterm

6 pages

Lecture 8

Lecture 8

108 pages

Lab 4

Lab 4

4 pages

Lecture 7

Lecture 7

52 pages

Lecture 20

Lecture 20

129 pages

Lecture 15

Lecture 15

132 pages

Lecture 9

Lecture 9

95 pages

Lecture 30

Lecture 30

108 pages

Lecture 17

Lecture 17

106 pages

Load more
Download OBJECT ORIENTED PROGRAMMING
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view OBJECT ORIENTED PROGRAMMING and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view OBJECT ORIENTED PROGRAMMING 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?