• Previous lecture: – Introduction to objects and classes • T oday’s lecture: – Defining a class • Properties • Constructor and other methods – Objects are passed by reference to functions • Announcements: – Test 2A due today 4:30pm EDT • Consulting hours, Piazza resume afterwards – Project 5 released, due next Thurs
Quiz: Object-oriented vocabulary Which of the following is incorrect? – Methods are functions that define a class’s A behavior – Variables store handles to objects, so two B different variables may reference the same object – Classes are instances of objects, each with their C own copy of property values – Constructors return handles to newly allocated D objects
classdef Interval < handle Multiple Interval objects properties left right 167.32 177.54 end left 3 left 4 methods function scale(self, f) . . . right 7 right 6 end Interval() Interval() function shift(self, s) . . . scale() scale() end shift() shift() function Inter = overlap(self, other) overlap() overlap() . . . end . . . end Every object (instance) contains every “ instance variable ” and every “ instance method ” defined in end the class. Every object has its own handle.
classdef Interval < handle The constructor method % An Interval has a left end and a right end properties T o create an Interval left object, use its class right end name as a function methods call: p = Interval(3,7) function Inter = Interval(lt, rt) % Constructor: construct an Interval obj Inter.left= lt; 167.32 Inter.right= rt; end left 3 Constructor , a special method with these jobs: function scale(self, f) • Automatically compute the handle of the new right 7 % Scale the interval by a factor f object; the handle must be returned. w= self.right - self.left; • Execute the function code (to assign values to self.right= self.left + w*f; Interval() end properties) end scale() Constructor is the only method that has the end name of the class.
A handle object is referenced by its handle p = Interval(3,7); r = Interval(4,6); A handle, also called a reference, p r is like an address; 167.32 177.54 it indicates the 167.32 177.54 memory location left 3 left 4 where the object is stored. right 7 right 6 Interval() Interval() scale() scale()
Value vs. reference classdef Pair < handle properties Arrays Object handles x y c= { [3, 1] }; c= { Pair(3, 1) }; end end a= c{1}; a= c{1}; a(2)= 4; a.y= 4; disp(c{1}(2)) disp(c{1}.y) 167.32 c: c: 3 1 167.32 x 3 y 1 a: a: 3 1 167.32
classdef Interval < handle Syntax for calling an % An Interval has a left end and a right end instance method properties left r = Interval(4,6); right r.scale(5) end methods function Inter = Interval(lt, rt) Method name % Constructor: construct an Interval obj Inter.left= lt; Reference of Inter.right= rt; the object end Argument for the whose second parameter method is to function scale(self, f) specified in function be % Scale the interval by a factor f header (f). Argument dispatched w= self.right - self.left; for first parameter self.right= self.left + w*f; (self) is absent because end it is the same as r, the end end owner of the method
Calling an object’s method (instance method) p = Interval(3,7); p r 167.32 177.54 r = Interval(4,6); r.scale(5) 167.32 177.54 left 3 left 4 right 7 right 6 Interval() Interval() The owner of the scale() scale() method to be dispatched Syntax: < reference> . < method >(< arguments for 2 nd thru last parameters >)
classdef Interval < handle Executing an instance method % An Interval has a left end and a right end properties left r = Interval(4,6); right r.scale(5) end disp(r.right) %What will it be? methods function Inter = Interval(lt, rt) % Constructor: construct an Interval obj r A: 5 177.54 Inter.left= lt; Inter.right= rt; B: 6 end 177.54 function scale(self, f) C: 14 left 4 % Scale the interval by a factor f w= self.right - self.left; right 6 D: 30 self.right= self.left + w*f; end end Interval() end scale()
classdef Interval < handle Executing an instance method % An Interval has a left end and a right end properties left r = Interval(4,6); right r.scale(5) end disp(r.right) %What will it be? methods function Inter = Interval(lt, rt) Function space of scale % Constructor: construct an Interval obj r 177.54 self Inter.left= lt; 177.54 Inter.right= rt; end 177.54 function scale(self, f) left 4 % Scale the interval by a factor f w= self.right - self.left; right 6 self.right= self.left + w*f; end end Interval() end scale()
classdef Interval < handle Executing an instance method % An Interval has a left end and a right end properties left r = Interval(4,6); right r.scale(5) end disp(r.right) %What will it be? methods function Inter = Interval(lt, rt) Function space of scale % Constructor: construct an Interval obj r 177.54 self Inter.left= lt; 177.54 Inter.right= rt; f end 5 177.54 function scale(self, f) w 2 left 4 % Scale the interval by a factor f w= self.right - self.left; right 6 self.right= self.left + w*f; end end Interval() end scale()
classdef Interval < handle Object is passed to a function % An Interval has a left end and a right end by reference properties left r = Interval(4,6); right r.scale(5) end disp(r.right) % updated value methods function Inter = Interval(lt, rt) Function space of scale % Constructor: construct an Interval obj r 177.54 self Inter.left= lt; 177.54 Inter.right= rt; f end 5 177.54 function scale(self, f) w 2 left 4 % Scale the interval by a factor f w= self.right - self.left; right 14 self.right= self.left + w*f; end Objects are passed to functions by reference . Changes to an end Interval() object’s property values made through the local reference ( self) end scale() stays in the object even after the local reference is deleted when the function ends.
Command Window workspace Function space of scale2 v 2 4 1 v 2 4 1 f 5 v= [2 4 1]; function scale2(v,f) scale2(v,5) % Scale v by a factor f disp(v) %??? v= v*f; Non-objects are passed to a function by value
Command Window workspace Function space of scale2 v 10 20 5 v 2 4 1 f 5 v= [2 4 1]; function scale2(v,f) scale2(v,5) % Scale v by a factor f disp(v) %??? v= v*f; Non-objects are passed to a function by value
Command Window workspace Function space of scale2 v 10 20 5 v 2 4 1 f 5 v= [2 4 1]; function scale2(v,f) scale2(v,5) % Scale v by a factor f disp(v) %NO CHANGE v= v*f; Non-objects are passed to a function by value
Objects are passed to a function by reference classdef Interval < handle r = Interval(4,6); : r.scale(5) methods disp(r.right) % updated value : function scale(self, f) % Scale the interval by a factor f w= self.right - self.left; self.right= self.left + w*f; end end end v= [2 4 1]; function scale2(v,f) scale2(v,5) % Scale v by a factor f disp(v) %NO CHANGE v= v*f; Non-objects are passed to a function by value
Syntax for calling an instance method: < reference> . < method >(< arguments for 2 nd thru last parameters >) classdef Interval < handle p = Interval(3,7); : methods r = Interval(4,6); : function scale(self, f) % Scale self by a factor f yesno= p.isIn(r); w= self.right - self.left; % Explicitly call self.right= self.left + w*f; end % p’s isIn method function tf = isIn(self, other) % tf is true if self is in other interval yesno= isIn(p,r); tf= self.left>=other.left && ... self.right<=other.right; % Matlab chooses the end % isIn method of one % of the parameters. end end
Method to find overlap between two Intervals function Inter = overlap(self, other) % Inter is overlapped Interval between self % and the other Interval. If no overlap then % Inter is empty array of class Interval.
Compare two intervals 1 redRight < blueRight 2 3 4 blueRight < redRight 5 6
1 The overlap’s left (OLeft) is the 2 rightmost of the two original lefts 3 4 5 6
1 The overlap’s left (OLeft) is the 2 rightmost of the two original lefts 3 The overlap’s right (ORight) is the 4 leftmost of the two original rights 5 6
1 The overlap’s left (OLeft) is the 2 rightmost of the two original lefts 3 The overlap’s right (ORight) is the 4 leftmost of the two original rights 5 No overlap if 6 OLeft > ORight
Implement overlap method DEMO
function Inter = overlap(self, other) % Inter is overlapped Interval between self % and the other Interval. If no overlap then % Inter is empty array of class Interval. Inter= Interval.empty(); left= max(self.left, other.left); right= min(self.right, other.right); if right-left > 0 Inter= Interval(left, right); end % Example use of overlap function end A= Interval(3,7); B= Interval(4,4+rand*5); X= A.overlap(B); Built-in function if ~isempty(X) isempty fprintf (’(% f,%f)\ n’, X.left,X.right) end
Recommend
More recommend