1 object oriented programming ras bodik, thibaud hottelier, james ide uc berkeley cs164:...

27
1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Upload: mohammed-sorsby

Post on 01-Apr-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

1

Object Oriented Programming

Ras Bodik, Thibaud Hottelier, James IdeUC Berkeley

CS164: Introduction to Programming Languages and Compilers Fall 2010

Page 2: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Objects (review from CS61B)

Why objects?abstraction: hide implementation under encapsulation

Why inheritance?reuse: specialization of an object’s behavior reuses its code

2

Page 3: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Our Design Rationale

We want to support objects.

Our language already supports closureswhich are similar in that they carry state and code

Can we build objects from this existing mechanism?

rather than adding support for objects into the language?

3

Page 4: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Single-Method Approach

4

Page 5: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Object is represented with a closure

function newObject (value) function (action, v) if (action == "get“) { value } else if (action == "set“) { value = v } else { error("invalid action")} } } 5

Page 6: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Use of single-method object

d = newObject(0) print d("get") --> 0 d("set", 10) print d("get") --> 10

6

Page 7: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Your homework (see the assigned reading)

Pros of single-method objects:

Cons of single method objects:

7

Page 8: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Table-of-Methods Approach

8

Page 9: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Object is a table of methods

function newAccount (initialBalance) def self = {balance = initialBalance} def withdraw (v) { self.balance = self.balance – v } def deposit (v) { self.balance = self.balance + v } def getBalance () { self.balance } { withdraw = withdraw, deposit = deposit, getBalance = getBalance} }

9

Page 10: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Use of this object

acc1 = newAccount(100.00)acc1.withdraw(40.00)print acc1.getBalance() --> 60

10

Page 11: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Discussion

This approach supports private data.Users of the object cannot access the balance except via objects methods.

Why is this useful?

How can we extend the object with private methods?

11

Page 12: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Objects as tables

12

Page 13: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Object is a table of attributes

Account = {balance = 0}

function Account.withdraw (v) { Account.balance = Account.balance - v}

Account.withdraw(100.00)

a = Account; Account = nila.withdraw(100.00) -- ERROR!

13

Page 14: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Syntactic sugar

Recall that p.f is p[“f”], which is get(p, “f”).

Recall that we need to distinguish between reading p.f and writing into p.f .

The former is translated into get, the latter into put.

14

Page 15: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Introduce self

function Account.withdraw (self, v) { self.balance = self.balance - v}

a1 = Account; Account = nila1.withdraw(a1, 100.00) -- OK

a2 = {balance=0, withdraw = Account.withdraw}a2.withdraw(a2, 260.00)

15

Page 16: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

The colon notation

function Account:withdraw (v) { self.balance = self.balance - v} a:withdraw(100.00)

16

Page 17: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

More syntactic sugar

17

Page 18: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Discussion

What is the inefficiency of our current objects?

too much space wasted by each object carrying its objects and fields that are constant across many objects

18

Page 19: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Meta-Methods

19

Page 20: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

The __index metamethod

When a lookup of a field fails, interpreter consolust the __index field:

20

Page 21: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Prototypes

21

Page 22: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Create an object

function Account:new (o) { -- create object if user does not provide one o = o or {} o.__index = self o}

a = Account:new({balance = 0})a:deposit(100.00)

22

Page 23: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Inheritance

23

Page 24: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Define a class

Account = {balance = 0} function Account:new (o) { o = o or {} o.__index = self o}function Account:deposit (v) { self.balance = self.balance + v} function Account:withdraw (v) { if (v > self.balance) { error"insufficient funds" } self.balance = self.balance - v} 24

Page 25: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Create subclass of Account

SpecialAccount = Account:new()s = SpecialAccount:new({limit=1000.00})s:deposit(100.00)

function SpecialAccount:withdraw (v) if (v - self.balance >= self:getLimit()) { error"insufficient funds" } self.balance = self.balance - v}

function SpecialAccount:getLimit () { self.limit or 0}

25

Page 26: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Discussion

Note how constant-value object attributes (fields) remain stored in the prototype until they are assigned

at which point the object stores the attribute rather than finding it in the prototype

26

Page 27: 1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Multiple Inheritance(see the reading assignment)

27