defining a new class defining some methods
play

Defining a new class Defining some methods Example: 3-D Points - PDF document

Defining a new class Defining some methods Example: 3-D Points instance methods: + anotherPoint | result | class definition: result := Point3D new. Object subclass: #Point3D result x: x + anotherPoint x. instanceVariableNames: x y z


  1. Defining a new class Defining some methods Example: 3-D Points instance methods: + anotherPoint | result | class definition: result := Point3D new. Object subclass: #Point3D result x: x + anotherPoint x. instanceVariableNames: ’x y z’ result y: y + anotherPoint y. classVariableNames: ’’ result z: z + anotherPoint z. poolDictionaries: ’’ ^ result category: ’Graphics-Primitives’ scaleBy: factor “ modifies receiver (unlike in Squeak) ” x := x * factor. No special syntax for class definition y := y * factor. • evaluate expression using Browser to build class z := z * factor. x ^ x x: newX x := newX. y ... z ... y: ... z: ... plus many other methods Craig Chambers 111 CSE 341 Craig Chambers 112 CSE 341 Class methods Using inheritance instead A class (e.g. Point3D ) is an object Define Point3D as a subclass of Point • it has methods it inherits (e.g. new ) • it can have user-defined methods class definition: Point subclass: #Point3D instanceVariableNames: ’z’ To create an instance of a class, send the class a new message: classVariableNames: ’’ p := Point3D new. poolDictionaries: ’’ category: ’Graphics-Primitives’ Can define your own class methods for e.g. initialized creation instance methods: In Point3D class methods: + anotherPoint x: x y: y z: z same as before scaleBy: factor | p | same as before p := self new. z ... z: ... p x: x. p y: y. p z: z. Summary: ^ p • inherit x and y instance variable declarations • inherit x , x: , y , y: , etc., methods A use: • add z , z: methods p := Point3D x: 3 y: 4 z: 5. • override + , scaleBy: methods Craig Chambers 113 CSE 341 Craig Chambers 114 CSE 341

  2. Sends to self Differential programming for methods If a message is sent to self , There’s redundancy in current implementation then method lookup starts at the object that self refers to, not the current class In Point : scaleBy: factor Example: In Point : x := x * factor. double y := y * factor. ^ self + self In Point3D : scaleBy: factor This behavior is crucial to object-oriented programming x := x * factor. y := y * factor. z := z * factor. Craig Chambers 115 CSE 341 Craig Chambers 116 CSE 341 Super sends A pitfall Can use super send to avoid code duplication: In Point : + anotherPoint | result | In Point3D : result := Point new. scaleBy: factor result x: x + anotherPoint x. super scaleBy: factor. result y: y + anotherPoint y. z := z * factor. ^ result Send to super is just like send to self , Current Point3D : except method lookup starts in superclass + anotherPoint • super can only appear as a message receiver | result | result := Point3D new. result x: x + anotherPoint x. result y: y + anotherPoint y. result z: z + anotherPoint z. ^ result “Better” Point3D : + anotherPoint | result | result := super + anotherPoint. result z: z + anotherPoint z. ^ result Craig Chambers 117 CSE 341 Craig Chambers 118 CSE 341

  3. Inserting sends to self Another example of inheritance: PolarPoint Increase reusability of Point + method Goal: define a 2-D point that represents values by replacing hard-wired constant with send to self using polar coordinates ( rho and theta ) In Point : Idea: implement by subclassing existing cartesian Point class + anotherPoint | result | class definition: result := self pointClass new. Point subclass: #PolarPoint result x: x + anotherPoint x. instanceVariableNames: ’rho theta’ result y: y + anotherPoint y. classVariableNames: ’’ ^ result poolDictionaries: ’’ pointClass category: ’Graphics-Primitives’ ^ Point instance methods: In Point3D : rho ... theta ... + anotherPoint rho: ... theta: ... | result | x result := super + anotherPoint. ^ rho * theta cos result z: z + anotherPoint z. y ^ result ^ rho * theta sin pointClass x: ... y: ... ^ Point3D Craig Chambers 119 CSE 341 Craig Chambers 120 CSE 341 A problem A solution Example: Old: | p1 p2 | + anotherPoint | result | p1 := PolarPoint new. result := self class new. p1 rho: 1. result x: x + anotherPoint x. p1 theta: 60. result y: y + anotherPoint y. p2 := PolarPoint new. ^ result p2 rho: 1.5. p2 theta: 170. New: + anotherPoint | result | p1 + p2 result := self class new. result x: self x + anotherPoint x. produces a message-not-understood error: result y: self y + anotherPoint y. sending + to an instance of UndefinedObject (i.e., nil ) ^ result Why? Theme: adding sends to self increases flexibility later Craig Chambers 121 CSE 341 Craig Chambers 122 CSE 341

  4. Abstract vs. concrete classes The interface Point defines both an interface and an implementation class definition: Object subclass: #Point instanceVariableNames: ’’ ... For better flexibility, split these two components apart instance methods: • abstract superclass containing methods + anotherPoint but no instance variables | result | • concrete subclass providing the instance variables and result := self class new. some accessor methods result x: self x + anotherPoint x. result y: self y + anotherPoint y. Abstract classes represent interfaces; can’t be instantiated result z: self z + anotherPoint z. Concrete classes flesh out abstract classes with full ^ result implementations scaleBy: factor self x: self x * factor. self y: self y * factor. self z: self z * factor. x self subclassResponsibility x: newX self subclassResponsibility y ... y: ... Craig Chambers 123 CSE 341 Craig Chambers 124 CSE 341 The implementation Another implementation class definition: class definition: Point subclass: #CartesianPoint Point subclass: #PolarPoint instanceVariableNames: ’x y’ ... instanceVariableNames: ’rho theta’ ... instance methods: instance methods: x ... y ... x: ... y: ... rho ... theta ... rho: ... theta: ... x ^ rho * theta cos y ^ rho * theta sin x: newValue ... y: newValue ... Craig Chambers 125 CSE 341 Craig Chambers 126 CSE 341

Recommend


More recommend