Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Chapter 4 Objectives
After you have read and studied this
chapter, you should be able to
Define an instantiable class with
multiple methods and constructors.
Differentiate the local and instance
variables.
Define and use value-returning
methods.
Distinguish private and public
methods.
Distinguish private and public data
members.
TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Fig. 4.1
A program template for a class
definition.
yenConverter
= new CurrencyConverter();
yenConverter.setExchangeRate(130.77);
markConverter = new CurrencyConverter( );
markConverter.setExchangeRate(1.792);
amountInYen
= yenConverter.fromDollar( 200 );
amountInMark
= markConverter.fromDollar( 200 );
amountInDollar
= yenConverter.toDollar( 10000 );
amountInMark
= markConverter.fromDollar(amountInDollar);
Fig. 4.2
Every object of a class has its own copy
of instance variables.
}
TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
value ) { }
) { }
double
dollar
{
double
Local
Local
Variables
Variables
amount, fee;
Parameter
Parameter
fee
= exchangeRate - feeRate;
amount
= dollar * fee;
return amount;
}
Fig. 4.3
Memory space for local variables and
parameters is allocated and erased.
Fig. 4.4
Memory space for the parameters is
allocated and erased.
Fig. 4.5
Calling a method belonging to the same
object vs. calling a method belonging
to a different object.
Fig. 4.6
How an object is passed to a method.
Fig. 4.7
How an object is passed to a method
(cont.).
Fig. 4.8
How an object is returned from a
method.
Fig. 4.9
How an object is returned from a
method.
Fig. 4.10
The program diagram for design
alternative 1.
Fig. 4.11
The program diagram for alternative
design 2.
Javadoc comments
Javadoc comments begin with /** and
end with */.
Javadoc tags are special markers that
begin with @. For example:
@author
@param
@return