Sie sind auf Seite 1von 3

Inheritance Rules for Generic Types

Generic classes, a few rules about inheritance and subtypes.


The test program in Example gathers up the various methods.
PairTest3.java
1. import java.util.*;
2.
3. public class PairTest3
4. {
5. public static void main(String[] args)
6. {
7.
Manager ceo = new Manager("Gus Greedy", 800000, 2003, 12, 15);
8.
Manager cfo = new Manager("Sid Sneaky", 600000, 2003, 12, 15);
9.
Pair<Manager> buddies = new Pair<Manager>(ceo, cfo);
10.
printBuddies(buddies);
11.
12.
ceo.setBonus(1000000);
13.
cfo.setBonus(500000);
14.
Manager[] managers = { ceo, cfo };
15.
16.
Pair<Employee> result = new Pair<Employee>();
17.
minMaxBonus(managers, result);
18.
System.out.println("first: " + result.getFirst().getName()
19.
+ ", second: " + result.getSecond().getName());
20.
maxMinBonus(managers, result);
21.
System.out.println("first: " + result.getFirst().getName()
22.
+ ", second: " + result.getSecond().getName());
23. }
24.
25. public static void printBuddies(Pair<? extends Employee> p)
26. {
27.
Employee first = p.getFirst();
28.
Employee second = p.getSecond();
29.
System.out.println(first.getName() + " and " + second.getName() + " ar
e buddies.");
30. }
31.
32. public static void minMaxBonus(Manager[] a, Pair<? super Manager> result)
33. {
34.
if (a == null || a.length == 0) return;
35.
Manager min = a[0];
36.
Manager max = a[0];
37.
for (int i = 1; i < a.length; i++)
38.
{
39.
if (min.getBonus() > a[i].getBonus()) min = a[i];
40.
if (max.getBonus() < a[i].getBonus()) max = a[i];
41.
}
42.
result.setFirst(min);
43.
result.setSecond(max);
44. }
45.
46. public static void maxMinBonus(Manager[] a, Pair<? super Manager> result)
47. {
48.
minMaxBonus(a, result);
49.
PairAlg.swapHelper(result); // OK--swapHelper captures wildcard type
50. }
51. }
52.

// x-x-x-x-x-x-x-x-x-x-x-x-x-xx-x-x-x-x-x-x-x-x-x-x-x-x-x
53. class PairAlg
54. {
55.
public static boolean hasNulls(Pair<?> p)
56.
{
57.
return p.getFirst() == null || p.getSecond() == null;
58.
}
59.
60.
public static void swap(Pair<?> p) { swapHelper(p); }
61.
62.
public static <T> void swapHelper(Pair<T> p)
63.
{
64.
T t = p.getFirst();
65.
p.setFirst(p.getSecond());
66.
p.setSecond(t);
67.
}
68. }
69.
// x-x-x-x-x-x-x-x-x-x-x-x-x-xx-x-x-x-x-x-x-x-x-x-x-x-x-x
70. class Employee
71. {
72.
public Employee(String n, double s, int year, int month, int day)
73.
{
74.
name = n;
75.
salary = s;
76.
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, d
ay);
77.
hireDay = calendar.getTime();
78.
}
79.
80.
public String getName()
81.
{
82.
return name;
83.
}
84.
85.
public double getSalary()
86.
{
87.
return salary;
88.
}
89.
90.
public Date getHireDay()
91.
{
92.
return hireDay;
93.
}
94.
95.
public void raiseSalary(double byPercent)
96.
{
97.
double raise = salary * byPercent / 100;
98.
salary += raise;
99.
}
100.
101.
private String name;
102.
private double salary;
103.
private Date hireDay;
104. }
105.

// x-x-x-x-x-x-x-x-x-x-x-x-x-xx-x-x-x-x-x-x-x-x-x-x-x-x-x
106. class Manager extends Employee
107. {
108.
/**
109.
@param n the employee's name
110.
@param s the salary
111.
@param year the hire year
112.
@param month the hire month
113.
@param day the hire day
114. */
115. public Manager(String n, double s, int year, int month, int day)
116. {
117.
super(n, s, year, month, day);
118.
bonus = 0;
119. }
120.
121. public double getSalary()
122. {
123.
double baseSalary = super.getSalary();
124.
return baseSalary + bonus;
125. }
126.
127. public void setBonus(double b)
128. {
129.
bonus = b;
130. }
131.
132. public double getBonus()
133. {
134.
return bonus;
135. }
136.
137. private double bonus;
138. }

Das könnte Ihnen auch gefallen