Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: Needing an analogy
|
Posted: Jun 30, 2002 12:59 AM
|
|
Hey, where's the analogy!?
How's this: main() is like your day: you wake up, stumble into your kitchen and fumble through the cupboard for your favorite coffee cup. The coffee cup is like a reference to an object. It is not of much use unless you put something in it. So you create some coffee and put it in the cup. Coffee is the (important) object and the cup is your means of access to it.
public class AnotherDay
{
public static void main( String [] args )
{
// Zzz...
Java coffeeCup = null; // The cup is empty.
coffeeCup = new Java(); // The cup is full.
coffeeCup.takeAGulp(); // The cup is half full... or is it half empty?
}
}
class Java
{
int percentFull = 100;
public void takeAGulp()
{
percentFull /= 2;
System.out.println( "Gulp! Ahh..." );
System.out.println( "("+ percentFull + "% coffee left)" );
}
}
(disclaimer: this is a very simplified analogy; had this been a real analogy, there would be many more objects, such as a cup object, a kitchen object, a coffee maker object, and on and on.)
|
|