Đang tải... (xem toàn văn)
Chapter 3 - Objects and classes. This chapter gives an introduction to the main Java and OOP concepts but does not require their full understanding. In terms of grasping the big picture, each student proceeds at his or her own pace. The same concepts are covered later in more depth. Here students get a general idea of how things are put together, get a feel for OOP, and work on exercises that require rearranging small pieces of code.
Java Methods Object-Oriented Programming and Data Structures 2nd AP edition with GridWorld Maria Litvin ● Gary Litvin C h a p t e r 3 Objects and Classes Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing All rights reserved Objectives: • See an example of a GridWorld program, written in OOP style, and discuss the types of objects used in it • Learn about the general structure of a class, its fields, constructors, and methods • Get a feel for how objects are created and how to call their methods • Learn a little about inheritance in OOP 32 OOP • An OO program models the application as a world of interacting objects • An object can create other objects • An object can call another object’s (and its own) methods (that is, “send messages”) • An object has data fields, which hold values that can change while the program is running 33 Objects • Can model real-world objects • Can represent GUI (Graphical User Interface) components • Can represent software entities (events, files, images, etc.) • Can represent abstract concepts (for example, rules of a game, a location on a grid, etc.) 34 Objects in the Bug Runner program GridWorld window Message display Menu bar, menus Scroll bar Grid Locations (in the grid) Bugs, flowers, and rocks Colors Control panel Buttons, slider 35 Classes and Objects • A class is a piece of the program’s source code that describes a particular type of objects OO programmers write class definitions • An object is called an instance of a class A program can create and use more than one object (instance) of the same class 36 Class Object • A blueprint for objects of a particular type Attributes • Defines the structure (number, types) of the attributes Behaviors • Defines available behaviors of its objects 37 Class: Car Attributes: String model Color color int numPassengers double amountOfGas Object: a car Attributes: model = "Mustang" color = Color.YELLOW numPassengers = amountOfGas = 16.5 Behaviors: Behaviors: Add/remove a passenger Get the tank filled Report when out of gas 38 Class • A piece of the program’s source code • Written by a programmer vs Object • An entity in a running program • Created when the program is running (by the main method or a constructor or another method) 39 Class vs • Specifies the structure (the number and types) of its objects’ attributes — the same for all of its objects • Specifies the possible behaviors of its objects Object • Holds specific values of attributes; these values can change while the program is running • Behaves appropriately when called upon 310 Constructors • Short procedures for creating objects of a class • • • • Always have the same name as the class Initialize the object’s fields May take parameters A class may have several constructors that differ in the number and/or types of their parameters 321 Constructors (cont’d) public class Location { private int row; private int col; The name of a constructor is always the same as the name of the class public Location (int r, int c) { A constructor can take parameters row = r; col = c; } } Initializes fields 322 Constructors (cont’d) // BugRunner.java Location loc = new Location(3, 5); public class Location { public Location (int r, int c) { } An object is created with the new operator The number, order, and types of parameters must match Constructor } 323 Constructors (cont’d) JButton go = new JButton("Go"); 324 Methods • Call them for a particular object: ActorWorld world = new ActorWorld(); Bug bob = new Bug(); world.add (new Location(3, 5), bob); bob.move( ); bob.turn( ); 325 Methods (cont’d) • The number and types of parameters (a.k.a arguments) passed to a method must match method’s parameters: public void drawString ( String msg, int x, int y ) { } g.drawString ("Welcome", 120, 50); 326 Methods (cont’d) • A method can return a value to the caller • The keyword void in the method’s header indicates that the method does not return any value public void drawString ( ) { } 327 Encapsulation and Information Hiding • A class interacts with other classes only through constructors and public methods • Other classes not need to know the mechanics (implementation details) of a class to use it effectively • Encapsulation facilitates team work and program maintenance (making changes to the code) 328 Methods (cont’d) • Constructors and methods can call other public and private methods of the same class • Constructors and methods can call only public methods of another class Class X Class Y private field public method public method private method 329 Inheritance • In OOP a programmer can create a new class by extending an existing class Superclass (Base class) subclass extends superclass Subclass (Derived class) 330 Inheritance (cont’d) Actor Bug extends Actor Bug UTurnBug extends Bug UTurnBug 331 A Subclass • inherits fields and methods of its superclass • can add new fields and methods • can redefine (override) a method of the superclass • must provide its own constructors, but calls superclass’s constructors • does not have direct access to its superclass’s private fields 332 public class UTurnBug extends Bug { public UTurnBug (Color bugColor) { setColor (bugColor); } public void turnAround () { turn(); turn(); turn(); turn(); // Or: setDirection (getDirection() + 180); } Constructor A new method } 333 Review: • Name a few objects used in GridWorld’s BugRunner • Name a few library classes used in GridWorld • What are import statements used for? • What is a field? A constructor? A method? • Which operator is used to construct an object? 334 Review (cont’d): • What is the difference between private and public methods? • Why are fields usually private? • What is inheritance? • Can a subclass add new fields? New methods? 335 ... organized into packages For example: java. util — miscellaneous utility classes java. awt — windowing and graphics toolkit javax.swing — GUI development package 3? ? 13 import • Full library class names... } By convention, the name of a class (and its source file) always starts with a capital letter (In Java, all names are case-sensitive.) 3? ?12 Libraries • Java programs are usually not written... (Derived class) 3? ?30 Inheritance (cont’d) Actor Bug extends Actor Bug UTurnBug extends Bug UTurnBug 3? ?31 A Subclass • inherits fields and methods of its superclass • can add new fields and methods