Java Reference
In-Depth Information
Display 14.8
Multiple Type Parameters
(part 2 of 2)
24 {
25
return
first;
26 }
27
public
T2 getSecond()
28 {
29
return
second;
30 }
31
public
String toString()
32 {
33
return
( "first: " + first.toString() + "\n"
34 + "second: " + second.toString() );
35 }
36
37
public boolean
equals(Object otherObject)
38 {
39
if
(otherObject = =
null
)
40
return false
;
41
else if
(getClass( ) != otherObject.getClass( ))
42
return false
;
43
else
44 {
45 TwoTypePair<T1, T2> otherPair =
46 (TwoTypePair<T1, T2>)otherObject;
47
return
(first.equals(otherPair.first)
48 && second.equals(otherPair.second));
49 }
50 }
51 }
The first
equals
is the
equals
of the type
T1
.
The second
equals
is the
equals
of the type
T2.
PITFALL: A Generic Class Cannot Be an Exception Class
If you begin an exception class definition as follows, you will get a compiler error
message:
public class
MyException<T>
extends
Exception
//Illegal
It is still illegal if you replace
Exception
with
Error
,
Throwable
, or any descendent
class of
Throwable
. You cannot create a generic class whose objects are throwable.
■