Java Reference
In-Depth Information
Display 14.11
A Derived Generic Class
(part 2 of 2)
10
setFirst(firstItem);
11
setSecond(secondItem);
12
}
13
public boolean
equals(Object otherObject)
14
{
15
if
(otherObject ==
null
)
16
return false
;
17
else if
(getClass() != otherObject.getClass())
18
return false
;
19
else
20
{
21
UnorderedPair<T> otherPair =
22
(UnorderedPair<T>)otherObject;
23
return
(getFirst().equals(otherPair.getFirst())
24
&& getSecond().equals(otherPair.getSecond()))
25
||
26
(getFirst().equals(otherPair.getSecond())
27
&& getSecond().equals(otherPair.getFirst()));
28
}
29
}
30
}
Suppose
HourlyEmployee
is a derived class of the class
Employee
. You might think that
an object of type
Pair<HourlyEmployee>
is also of type
Pair<Employee>
. You might think
that, but you would be wrong. If
G
is a generic class, there is no relationship between
G<A>
and
G<B>
, no matter what the relationship is between the classes
A
and B.
Display 14.12
Using
UnorderedPair
(part 1 of 2)
1
public class
UnorderedPairDemo
2{
3
public static void
main(String[] args)
4
{
5
UnorderedPair<String> p1 =
6
new
UnorderedPair<String>("peanuts", "beer");
7
UnorderedPair<String> p2 =
8
new
UnorderedPair<String>("beer", "peanuts");
9
if
(p1.equals(p2))
10
{
(continued)