130 likes | 269 Vues
Objects and Classes: creating simulations. Powerpoint created by Beverly McGuire. Objects. Objects are the things that do the action or are acted upon in a simulation. Objects can have data (fields) and behavior (methods).
E N D
Objects and Classes: creating simulations Powerpoint created by Beverly McGuire
Objects • Objects are the things that do the action or are acted upon in a simulation. • Objects can have data (fields) and behavior (methods). • Every object is an instance of a class (its classification or type). • The class creates the objects since it knows how big the object needs to be (how many and what type of fields). • All objects keep a reference to the class that created them (they know their type).
What does this mean? • If you go to a restaurant you may be greeted by the greeter and shown to your table. • The waiter will take your order and bring you your food. • The chef will cook the food. If we want to make a simulation of this we will have objects of the type: Greeter, Waiter, Chef, Customer, Order, Food, and so on. • We can have more than one object of each class. • There will usually be several waiters and many customers.
What are Objects used for? • Classes define the data (fields) that objects of that classification will have and the things that they will be able to do (methods). • Classes create objects. Objects act in the simulation.
Analogies: • Job descriptions are like classes. • Workers in those jobs are like the objects. • A class is also like a blueprint or cookie cutter. • Objects are like houses built based on the blueprints or like cookies made with the cookie cutter.
How Create an Object: • You ask the class to create a new object using the new keyword: • new Class(parameterlist) • You can create a new String object by the following: • String name = “Susan";
An Example of Creating an Object: new World(); new Turtle(new World());
Creating a Class: • Classes are defined in files. • Each class is usually defined in its own file. • The file name is the same as the class name with an extension of ".java".
Creating a Class:(cont.) • You define a class using the class keyword: visibility class Name or visibility class Name extends ParentClass • visibility is usually public meaning all other classes can use it
Creating a Class:(cont.) • Name is the name of the class. By convention each word in a class name starts with an uppercase letter like SimpleTurtle. • ParentClass is the name of the class this class inherits from. If a parent class isn't specified the parent is Object.
Creating a Class Common Errors: • Forgetting to declare a variable to hold a reference to a new object • If you create a new object but don't declare a variable to keep track of the reference you won't have any way to refer to it again. The object will be garbage collected and the memory it uses will be released. • new Turtle(new World());
Creating a Class Common Errors: • Not declaring a class in a file with the same name • Don’t try to save out a class into a new file with a different name. The name of the class and the file must match.
Special thanks to Barb Ericson for providing Concept Cards for this presentation