Professional Documents
Culture Documents
4/10/2013
Previous lecture:
Introduction to objects and classes
Object-Oriented Programming First design and define the classes (of the objects)
Identify the properties (data) and actions (methods, i.e., functions) of each class
Todays lecture:
Defining a class
Constructor Methods
Announcements:
Prelim 2 will be on Tues
Then create the objects (from the classes) that are then used, that interact with one another
Class Interval
left, right
properties left right end methods function scale(self, f) ... end function shift(self, s)
An Interval object
167.32 Thehandle orreference oftheobject
classdef Interval < handle properties left right end methods function scale(self, f) ... end function shift(self, s) ... end function Inter = overlap(self, other) ... end function Inter = add(self, other) ... end
The constructor method To create an Interval object, use its class name as a function call: p = Interval(3,7)
167.32
classdef Interval<handle %AnIntervalhasaleftendandarightend properties left right end methods function Inter=Interval(lt,rt) %Constructor:constructanIntervalobj Inter.left=lt; Inter.right=rt; end
Theconstructor isaspecialmethodwhose function scale(self,f) mainjobsare to % Scaletheintervalbyafactorf computethe handle of thenewobject, w= self.right self.left; self.left w*f; executetheself.right= function code+ (to assign end valuesto properties), and end returnend thehandleoftheobject. Constructorhasthenameoftheclass.
CS1112Lecture22
4/10/2013
classdef Interval<handle %AnIntervalhasaleftendandarigh properties left right end methods function Inter=Interval(lt,rt) %Constructor:constructanInte Inter.left=lt; Inter.right=rt; end function scale(self,f) %Scaletheintervalbyafactorf w=self.right self.left; self.right=self.left +w*f; end end end
classdef Interval<handle %AnIntervalhasaleftendandarigh properties left right end methods function Inter=Interval(lt,rt) %Constructor:constructanInte Inter.left=lt; Inter.right=rt; end
177.54
self
177.54
177.54
self
177.54
177.54
f 5 w2
177.54
f 5 w2
function scale(self,f) %Scaletheintervalbyafactorf w=self.right self.left; self.right=self.left +w*f; end Objectsarepassedtofunctions by reference.Changes toan end objectspropertyvaluesmadethrough thelocalreference(self) end staysintheobjectevenafterthelocalreferenceisdeletedwhen thefunction ends.
v 2
v 10 20 1 f 5
end end
CS1112Lecture22
4/10/2013
The overlaps left (OLeft) is the rightmost of the two original lefts The overlaps right (ORight) is the leftmost of the two original rights No overlap if OLeft > ORight
Lecture22 21
function Inter = overlap(self, other) % Inter is overlapped Interval between self % and the other Interval. If no overlap then % self is empty 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);
Builtin function
isempty
April5,2007
April5,2007
Overloading built-in functions You can change the behavior of a built-in function for an object of a class by implementing a function of the same name in the class definition Called overloading A typical built-in function to overload is disp
Specify which properties to display, and how, when the argument to disp is an object Matlab calls disp when theres no semi-colon at the end of an assignment statement
SeeInterval.m
classdef Interval<handle %AnIntervalhasaleftendandarightend properties left right end methods function Inter=Interval(lt,rt) %Constructor:constructanIntervalobj Inter.left=lt; Inter.right=rt; end function scale(self,f) %Scaletheintervalbyafactorf w=self.right self.left; self.right=self.left +w*f; end end end
Constructorreturnsa referencetotheclassobject
Constructor
Eachinstancemethodsfirst parametermustbeareference Instance methods totheinstance (object)itself Usekeywordendforclassdef, properties, methods, function.
(functions)