System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə59/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   55   56   57   58   59   60   61   62   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

superclass) is also within bounds.
You can also specify a lower bound for a wildcard by adding a super clause to a
wildcard declaration. Here is its general form:
subclass>
In this case, only classes that are superclasses of subclass are acceptable arguments.
This is an inclusive clause.
GENERIC METHODS
As the preceding examples have shown, methods inside a generic class can make use of
a class’ type parameter and are, therefore, automatically generic relative to the type
parameter. However, it is possible to declare a generic method that uses one or more
type parameters of its own. Furthermore, it is possible to create a generic method that
is enclosed within a nongeneric class.
The following program declares a nongeneric class called GenericMethodDemo and
a static generic method within that class called arraysEqual( ). This method


determines if two arrays contain the same elements, in the same order. It can be used to
compare any two arrays as long as the arrays are of the same or compatible types and
the array elements are, themselves, comparable.
The output from the program is shown here:
Let’s examine arraysEqual( ) closely. First, notice how it is declared by this line:


The type parameters are declared before the return type of the method. Also note that T
extends Comparable. Comparable is an interface declared in java.lang. A
class that implements Comparable defines objects that can be ordered. Thus,
requiring an upper bound of Comparable ensures that arraysEqual( ) can be used
only with objects that are capable of being compared. Comparable is generic, and its
type parameter specifies the type of objects that it compares. (Shortly, you will see how
to create a generic interface.) Next, notice that the type V is upper­bounded by T. Thus,
V must be either the same as type T or a subclass of T. This relationship enforces that
arraysEqual( ) can be called only with arguments that are comparable with each
other. Also notice that arraysEqual( ) is static, enabling it to be called independently
of any object. Understand, though, that generic methods can be either static or
nonstatic. There is no restriction in this regard.
Now, notice how arraysEqual( ) is called within main( ) by use of the normal call
syntax, without the need to specify type arguments. This is because the types of the
arguments are automatically discerned, and the types of T and V are adjusted
accordingly. For example, in the first call:
the element type of the first argument is Integer, which causes Integer to be
substituted for T. The element type of the second argument is also Integer, which
makes Integer a substitute for V, too. Thus, the call to arraysEqual( ) is legal, and
the two arrays can be compared.
Now, notice the commented­out code, shown here:
If you remove the comments and then try to compile the program, you will receive an
error. The reason is that the type parameter V is bounded by T in the extends clause in
V’s declaration. This means that V must be either type T or a subclass of T. In this case,
the first argument is of type Integer, making T into Integer, but the second argument
is of type Double, which is not a subclass of Integer. This makes the call to
arraysEqual( ) illegal, and results in a compile­time type­mismatch error.
The syntax used to create arraysEqual( ) can be generalized. Here is the syntax for a
generic method:


In all cases, type­param­list is a comma­separated list of type parameters. Notice that
for a generic method, the type parameter list precedes the return type.
GENERIC CONSTRUCTORS
A constructor can be generic, even if its class is not. For example, in the following
program, the class Summation is not generic, but its constructor is.
The Summation class computes and encapsulates the summation of the numeric
value passed to its constructor. Recall that the summation of N is the sum of all the
whole numbers between 0 and N. Because Summation( ) specifies a type parameter
that is bounded by Number, a Summation object can be constructed using any
numeric type, including Integer, Float, or Double. No matter what numeric type is
used, its value is converted to Integer by calling intValue( ), and the summation is
computed. Therefore, it is not necessary for the class Summation to be generic; only a
generic constructor is needed.
GENERIC INTERFACES


As you saw in the GenericMethodDemo program presented earlier, an interface can
be generic. In that example, the standard interface Comparable was used to
ensure that elements of two arrays could be compared. Of course, you can also define
your own generic interface. Generic interfaces are specified just like generic classes.
Here is an example. It creates an interface called Containment, which can be
implemented by classes that store one or more values. It declares a method called
contains( ) that determines if a specified value is contained by the invoking object.


The output is shown here:


Although most aspects of this program should be easy to understand, a couple of key
points need to be made. First, notice that Containment is declared like this:
In general, a generic interface is declared in the same way as a generic class. In this
case, the type parameter T specifies the type of objects that are contained.
Next, Containment is implemented by MyClass. Notice the declaration of MyClass,
shown here:
In general, if a class implements a generic interface, then that class must also be
generic, at least to the extent that it takes a type parameter that is passed to the
interface. For example, the following attempt to declare MyClass is in error:
This declaration is wrong because MyClass does not declare a type parameter, which
means that there is no way to pass one to Containment. In this case, the identifier T
is simply unknown and the compiler reports an error. Of course, if a class implements a

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   55   56   57   58   59   60   61   62   ...   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ə