DOC PREVIEW
MIT 6 001 - Object-Oriented Programming and Interpretation

This preview shows page 1 out of 3 pages.

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

Unformatted text preview:

6.001 Tutorial 9Structure and Interpretation of Computer Programs May 1, 2006Object-Oriented Programming and InterpretationThe OOP Onion• Object system – The underlying mechanism that makes objects work (staff pr ovided, or, morecommonly, language provided)• Objects – The implementation of objects on top of the object system (specific to the program).Generally doesn’t require knowledge of wh at’s making the objects system work.• Object user – Uses the objects with no knowledge of what’s inside the objects.The OOP SystemWith our full-blown object system, here’s what the code for a class looks like:; ; D e f i n e t h e c l a s s c o n s t r u c t o r( d e f i n e ( c re at e − TYPE ar g1 arg 2 . . . )( c r e a t e − i n s t a n c e TYPE ar g1 ar g2 . . . ) ); ; D e f i n e t h e c l a s s( d e f i n e (TYPE s e l f ar g1 ar g2 . . . ); ; C a p t u r e i n t e r n a l s t a t e , i n c l u d i n g i n s t a n c e s o f s u p e r c l a s s e s t o; ; d e l e g a t e t o . I f t h e r e a r e no b e t t e r s u p e r c l a s s e s , don ’ t f o r g e t; ; t o i n h e r i t f rom r o o t − o b j e c t( l e t ( ( s up e r1 − p a rt ( s u pe r 1 s e l f a r g s . . . ) )( s up e r2 − p a rt ( su p e r 2 s e l f a r g s . . . ) ); ; O t h er s u p e r c l a s s e s and l o c a l s t a t e); ; T h is f u n c t i o n h as t o r e t u r n t h e h a n d l e r f o r c l a s s o f t y p e , s o; ; c r e a t e t h a t h a n d l e r( make− handler; ; The h a n d l e r n e e d s t o know t h e name o f t h i s o b j e c t ’ s t y p e’ TYPE; ; make− methods b u n d l e s t o g e t h e r t h e m et h o d s f o r t h e h a n d l e r( make− methods’METHOD1 ( lambda ( a r g s . . . ) . . . )’METHOD2 ( lambda ( a r g s . . . ) . . . ) ); ; F i n a l l y , t h e h a n d l e r n e e d s t o know w he r e t o go f o r m e s s a g e s; ; t h a t w e ren ’ t d e f i n e d h e r esu p er 1− p ar tsu p er 2− p ar t ) ) ); ; I n s t a n t i a t e t h e c l a s s( d e f i n e i n s t ( c r ea te − TYPE ar g 1 a r g 2 . . . ) ); ; Ask t h e i n s t a n c e t o do s o m e t h i n g ( i e , p a s s i t a m e s s ag e )( a sk i n s t ’METHOD1 a rg 1 a r g2 . . . ); ; Or y o u can g e t t h e met hod d i r e c t l y as a p r o c e d u r e ( u s e f u l in; ; c o n j u n c t i o n w i t h h i g h e r o r d e r p r o c e d u r e s , s i n c e t h i s a l l o w s you; ; t o p a s s m e ss ag e h a n d l e r s / m et h od s a r ou n d l i k e r e g u l a r p r o c e d u r e s )( d e f i n e met hod− of− i ns t ( get− method i n s t ’METHOD1) )( me t hod − of− i nst ar g1 ar g 2 . . . )Table-Driven ProgrammingData-directed programming lets u s write an operation that could do different things dependingon the typ e of its arguments. O bject-oriented programming lets us write a data type that doesdifferent things depen ding on the operation. Table-driven programming offers greater flexibility byallowing each operator to be defined multiple times, each time for a different typ e.16.001 SICP Object-Oriented Programming and Interpretationask self Versus ask partWhat do the last three expressions evaluate to?( de f i n e ( cr ea te − fo o ) ( cr e a t e − i n s t an c e foo ) )( de f i n e ( foo s e l f )( l e t ( ( root− p art ( ro o t s e l f ) ) )( make− handler ’ foo( make− methods ’A (lambda ( ) 1 )’B (lambda ( ) 2 ) )root− p art ) ) )( de f i n e ( c reate− bar ) ( cr e a t e − i n stan c e bar ))( de f i n e ( bar s e l f )( l e t ( ( foo− part ( foo s e l f ) ) )( make− handler ’ bar( make− methods ’A (lambda ( ) 3 )’B (lambda ( ) (+ ( ask foo− part ’B)( ask …


View Full Document

MIT 6 001 - Object-Oriented Programming and Interpretation

Documents in this Course
Quiz 1

Quiz 1

6 pages

Databases

Databases

12 pages

rec20

rec20

2 pages

Quiz II

Quiz II

15 pages

Streams

Streams

5 pages

Load more
Download Object-Oriented Programming and Interpretation
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 Interpretation 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 and Interpretation 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?