Java Reference
In-Depth Information
Students who have prior experience with programming in another programming
language are used to programming functions. A function carries out an action. In
object-oriented programming, the actions appear as methods. Each method, however,
belongs to a class. Classes are collections of objects, and objects are not actionsȌ
they are entities. So you have to start the programming activity by identifying objects
and the classes to which they belong.
Remember the rule of thumb from Chapter 2 : Class names should be nouns, and
method names should be verbs.
A class should represent a single concept from the problem domain, such as
business, science, or mathematics.
What makes a good class? Most importantly, a class should represent a single
concept. Some of the classes that you have seen represent concepts from mathematics:
ȗ Point
ȗ Rectangle
ȗ Ellipse
336
337
Other classes are abstractions of real-life entities.
ȗ BankAccount
ȗ CashRegister
For these classes, the properties of a typical object are easy to understand. A
Rectangle object has a width and height. Given a BankAccount object, you can
deposit and withdraw money. Generally, concepts from the part of the universe that a
program concerns, such as science, business, or a game, make good classes. The
name for such a class should be a noun that describes the concept. Some of the
standard Java class names are a bit strange, such as Ellipse2D.Double , but you
can choose better names for your own classes.
Another useful category of classes can be described as actors. Objects of an actor
class do some kinds of work for you. Examples of actors are the Scanner class of
Chapter 4 and the Random class in Chapter 6 . A Scanner object scans a stream for
Search WWH ::




Custom Search