80 likes | 185 Views
CPSC 203 Tutorial. Xin Liu Nov 24, 2010. Object-oriented programming. A mimic of real world The world is composed of objects An object has some properties and methods and functions Components of OOP Classes The types/templates of objects Objects The instances of classes
E N D
CPSC 203 Tutorial Xin Liu Nov 24, 2010
Object-oriented programming • A mimic of real world • The world is composed of objects • An object has some properties and methods and functions • Components of OOP • Classes • The types/templates of objects • Objects • The instances of classes • Different objects can be created from the same class • The objects share the same methods but can have different names and properties • Methods • A method is an action that an object can take, actually composed of a sequence of instructions.
Exercise • Create two objects from the camel class, named as Camelo and Camely. Camelo has red ears; Camely has blue ears. Camelo is bigger; Camely is smaller.
More on methods • Build-in methods • User-defined methods • Class-level methods • The actions of an object • E.g. Iceskater.roll and Bunny.hop • World-level methods • The actions involving more than one objects • E.g. World.Iceskater discusses with Snowman • World.my first method is the “main program” we used to work on
Exercise • Go through Tutorial 2.
Exercise • Download and open lab4ggypt.a2w • Create a class-level method on Pharaoh, getScared • Beard enlarged by 1.2; nose enlarged by1.2; hat turns 1 rev; says “Aaaaah!” • Create a world-level method, getScared • Camera focuses on the Pharaoh • Show the mummy scared • Call world.getScared from world.myFirstMethod
Exercise • Add a class-level method, getScared, to Mummy • Head grows by 1.2 and then shrinks by 1/1.2; yells “Oooooh!” at the same time. • Edit the world-level method, getScared • Focus the camera to the mummy • Call mummy.getScared • Add a dummy object at the camera’s original position • Edit the world-level method, getScared • Turn the camera back
Summary • Two class-level methods • One world-level method • A neat program is obtained by splitting the code into class and world-level methods