System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə22/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   18   19   20   21   22   23   24   25   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

ret­type nameparameter­list ) {
// body of method
}
Here, ret­type specifies the type of data returned by the method. This can be any valid
type, including class types that you create. If the method does not return a value, its
return type must be void. The name of the method is specified by name. This can be
any legal identifier other than those already used by other items within the current
scope. The parameter­list is a sequence of type and identifier pairs separated by
commas. Parameters are essentially variables that receive the value of the arguments
passed to the method when it is called. If the method has no parameters, the parameter
list will be empty.
Adding a Method to the Vehicle Class
As just explained, the methods of a class typically manipulate and provide access to the
data of the class. With this in mind, recall that main( ) in the preceding examples
computed the range of a vehicle by multiplying its fuel consumption rate by its fuel
capacity. While technically correct, this is not the best way to handle this computation.
The calculation of a vehicle’s range is something that is best handled by the Vehicle
class itself. The reason for this conclusion is easy to understand: the range of a vehicle
is dependent upon the capacity of the fuel tank and the rate of fuel consumption, and


both of these quantities are encapsulated by Vehicle. By adding a method to Vehicle
that computes the range, you are enhancing its object­oriented structure. To add a
method to Vehicle, specify it within Vehicle’s declaration. For example, the following
version of Vehicle contains a method called range( ) that displays the range of the
vehicle.
This program generates the following output:


Let’s look at the key elements of this program, beginning with the range( ) method
itself. The first line of range( ) is
This line declares a method called range that has no parameters. Its return type is
void. Thus, range( ) does not return a value to the caller. The line ends with the
opening curly brace of the method body.
The body of range( ) consists solely of this line:
This statement displays the range of the vehicle by multiplying fuelcap by mpg. Since
each object of type Vehicle has its own copy of fuelcap and mpg, when range( ) is
called, the range computation uses the calling object’s copies of those variables.
The range( ) method ends when its closing curly brace is encountered. This causes
program control to transfer back to the caller.
Next, look closely at this line of code from inside main( ):
This statement invokes the range( ) method on minivan. That is, it calls range( )
relative to the minivan object, using the object’s name followed by the dot operator.
When a method is called, program control is transferred to the method. When the
method terminates, control is transferred back to the caller, and execution resumes
with the line of code following the call.
In this case, the call to minivan.range( ) displays the range of the vehicle defined by
minivan. In similar fashion, the call to sportscar.range( ) displays the range of the
vehicle defined by sportscar. Each time range( ) is invoked, it displays the range for
the specified object.
There is something very important to notice inside the range( ) method: the instance
variables fuelcap and mpg are referred to directly, without preceding them with an
object name or the dot operator. When a method uses an instance variable that is
defined by its class, it does so directly, without explicit reference to an object and


without use of the dot operator. This is easy to understand if you think about it. A
method is always invoked relative to some object of its class. Once this invocation has
occurred, the object is known. Thus, within a method, there is no need to specify the
object a second time. This means that fuelcap and mpg inside range( ) implicitly
refer to the copies of those variables found in the object that invokes range( ).
RETURNING FROM A METHOD
In general, there are two conditions that cause a method to return—first, as the range(
) method in the preceding example shows, when the method’s closing curly brace is
encountered. The second is when a return statement is executed. There are two forms
of return—one for use in void methods (those that do not return a value) and one for
returning values. The first form is examined here. The next section explains how to
return values.
In a void method, you can cause the immediate termination of a method by using this
form of return:
return ;
When this statement executes, program control returns to the caller, skipping any
remaining code in the method. For example, consider this method:
Here, the for loop will only run from 0 to 5, because once i equals 5, the method
returns. It is permissible to have multiple return statements in a method, especially
when there are two or more routes out of it. For example:


Here, the method returns if it is done or if an error occurs. Be careful, however, because
having too many exit points in a method can destructure your code; so avoid using
them casually. A well­designed method has well­defined exit points.
To review: A void method can return in one of two ways—its closing curly brace is
reached, or a return statement is executed.
RETURNING A VALUE
Although methods with a return type of void are not rare, most methods will return a
value. In fact, the ability to return a value is one of the most useful features of a method.
You have already seen one example of a return value: when we used the sqrt( )
function to obtain a square root.
Return values are used for a variety of purposes in programming. In some cases, such
as with sqrt( ), the return value contains the outcome of some calculation. In other
cases, the return value may simply indicate success or failure. In still others, it may
contain a status code. Whatever the purpose, using method return values is an integral
Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   18   19   20   21   22   23   24   25   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə