Prev | Current Page 212 | Next

Carl Reynolds and Paul Tymann

"Schaum's Outline of Principles of Computer Science"

out.println(
Automobile.getCount() + " Automobiles" );
System.out.println( economy );
System.out.println( family );
System.out.println( sports );
}
}
The class AutomobileFactory can be executed directly, because it has a main method coded in
the standard way. The main method declares three variables to be of type Automobile (notice you can
declare several variables of the same type on one line, as we did here), and then it creates three Automobile
objects by using the new keyword to invoke the constructor for the Automobile class three different
times.
The first println statement calls the static method getCount() by specifying the class name
Automobile, followed by a dot (period), followed by the method name getCount(). The getCount()
method will return the number 3, because each time the constructor of the Automobile class generates a new
Automobile, it increments its static count variable.
CHAP. 5] PROGRAMMING IN JAVA 81
Finally, the three println statements at the end print a text representation of each Automobile. Such
use of an object??™s name, such as economy, inside println causes the JVM to use the toString() method
of the class. When that happens here, the result on the screen is this:
3 Automobiles
2006 Kia Rio
2002 VW Passat
2005 Ford Mustang
Inheritance
OO programming makes it easy to add functionality to software without rewriting the code one has already
written and tested.


Pages:
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
Projekty domów texas holdem zasady wizualizacje teksty piosenek ciekawe zdania