Java Reference
In-Depth Information
Display 14.7
Using Our Ordered Pair Class and Automatic Boxing
(part 2 of 2)
Sample Dialogue
Enter two numbers:
42 24
You guessed the secret numbers
in the correct order!
TIP: A Class Definition Can Have More Than One Type Parameter
A generic class definition can have any number of type parameters. The multiple type
parameters are listed in angular brackets just as in the single type parameter case, but
are separated by commas. For example, in Display 14.8, we have rewritten the class
Pair
so the first and second items of a pair can be of different types. In Display 14.9,
we give a simple example of using our generic class with two type parameters.
■
Display 14.8
Multiple Type Parameters
(part 1 of 2)
1
public class
TwoTypePair<T1, T2>
2 {
3
private
T1 first;
4
private
T2 second;
5
public
TwoTypePair()
6 {
7 first =
null
;
8 second =
null
;
9 }
10
public
TwoTypePair(T1 firstItem, T2 secondItem)
11 {
12 first = firstItem;
13 second = secondItem;
14 }
15
public void
setFirst(T1 newFirst)
16 {
17 first = newFirst;
18 }
19
public void
setSecond(T2 newSecond)
20 {
21 second = newSecond;
22 }
23
public
T1 getFirst()
(continued)