1 / 13

Objects and Classes: creating simulations

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).

Télécharger la présentation

Objects and Classes: creating simulations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objects and Classes: creating simulations Powerpoint created by Beverly McGuire

  2. 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).

  3. 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.

  4. 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.

  5. 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.

  6. 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";

  7. An Example of Creating an Object: new World(); new Turtle(new World());

  8. 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".

  9. 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

  10. 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.

  11. 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());

  12. 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.

  13. Special thanks to Barb Ericson for providing Concept Cards for this presentation

More Related