Download presentation
Presentation is loading. Please wait.
Published byEliezer Godfrey Modified over 9 years ago
1
Objectives Understand grammar of OOP Understand equivalent Java code Discuss different implementations of classes and objects
2
Class Declarations ::={ }* a-program (class-decls body) ::= class extends { field }* { }* a-class-decl (class-name super-name field-ids method-decls) ::= method ( { }* (,) ) a-method-decl (method-name ids body)
3
Object Expressions ::= new ( { }* (,) ) new-object-exp (class-name rands) ::= send ( { }* (,) ) method-app-exp (obj-exp method-name rands) ::= super ( { }* (,) ) super-call-exp (method-name rands)
4
How do we do this in Java? class c2 extends c1Public class c2 extends c1 { field xPublic int x method initialize() set x = 1Public c2() { x = 1; } method m1 (a) +(x, a)Public int m1(int a) { return x+a; } method m2 (c) send self m1 (+(c, 4))Public int m2(int c) { return m1(c+4); } method m3 (a, b) super m1 (a, b)Public int m3(int a, int b) {return super.m1(a,b); } let o1 = new c2() inPublic static void main (String[] args) { c2 o1 = new c2(); send o1 m2 (3)o1.m2(3); }
5
How do we implement this? “idea” Class Declarations Program Body store class definitions access classes and objects when executing program Method Bodies Object Representation Class Representation Base code is in 5-3.scmImplementation code is in 5-4-x.scm
6
Interface to Classes Class Declarations elaborate-class-decls! (build repository of classes) Program Body super send new find-method-and-apply (perform send) new-object (allocate new object) object->class-name (what kind of object am I?) Method Bodies Object Representation Class Representation
7
Simple Implementation 5.4.1 object is list of class-name / fields “parts” How do we implement: (elaborate-class-decls! c-decls) (object->class-name o3_rep) (find-method-and-apply ‘m1 ‘c3 o3_rep ‘(7 8)) (new-object ‘c3) Object Representation (o3) a-part c3class-name fields xz a-part c2class-name fields y a-part c1class-name fields xy 3231221211 Class Representation (c3) a-class-decl c3class-name super-namec2 field-ids method-decls zx a-method-decl m1method-name method-ids expmethod-body vu directly from grammar
8
Flatten Object Fields 5.4.2 Object Representation (o3) a-part c3class-name fields xzyxy How do we implement: (elaborate-class-decls! c-decls) (object->class-name o3_rep) (find-method-and-apply ‘m1 ‘c3 o3_rep ‘(7 8)) (new-object ‘c3) 3231221211 c1c3c2 search from right Class Representation (c3) a-class-decl c3class-name super-namec2 field-ids method-decls zx a-method-decl m1method-name method-ids expmethod-body vu directly from grammar
9
Class Representation (c3) Flatten Class Fields 5.4.3 Object Representation (o3) a-part c3class-name fields xzyxy 3231221211 c1c3c2 a-class c3class-name super-namec2 field-length5 field-ids methods zxyyx a-method method-decl super-namec2 field-ids zxyyx a-method-decl m1method-name method-ids expmethod-body vu How do we implement: (elaborate-class-decls! c-decls) (object->class-name o3_rep) (find-method-and-apply ‘m1 ‘c3 o3_rep ‘(7 8)) (new-object ‘c3) 5.4.4 flattens class methods
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.