Prev | Current Page 210 | Next

Carl Reynolds and Paul Tymann

"Schaum's Outline of Principles of Computer Science"


Variables and methods will always be instance variables and methods, unless you specifically label them
static. If you label a variable or method static, the variable or method will exist only at the level of the
class, and will be shared among the instances.
Below is our example Automobile class. Notice that there is no main method in the Automobile
class. This class cannot be executed directly. If we were to add a public main method, the class could be executed,
and whatever the main method were coded to accomplish would be done. However, this Automobile
class is intended to be a ???factory??? to create Automobile objects for use by other programs and classes,
rather than to be a program to be run by itself. You can think of the Automobile class as creating a new data
type??”Automobile.
Notice also that in the class Automobile there is a method named Automobile; the name of the
method is the same as the name of the class. The method named Automobile is the constructor for the class.
Whenever another program needs an instance of an Automobile, the other program will use the new keyword
to request a new Automobile. We can tell from the Automobile constructor that the constructor
expects four parameters whenever a new Automobile is requested; these are the make, the model, the year,
and the horsepower.


Pages:
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
perfumy fm Stal rezerwacja hoteli pomiary wiatru Nike Buty Męskie Air Flex Trainer Lea