C:\Common\_Project_Source_\Towson\ProgrammingSamples\src\Generics\GenericClass.java |
1
2
3
4
5
6
7
8
9
10
11
12
13 package Generics;
14
15
16 @author <adam@adamconover.com>
17
18 public class GenericClass {
19
20
21
22 @param
23
24 public static void main(String[] args) {
25 Fuji f1 = new Fuji(335);
26 Orange f2 = new Orange(275);
27 Sunkist f3 = new Sunkist();
28
29 MySimpleContainer<Apple> appleContainer = new MySimpleContainer<Apple>(f1);
30 MyComparableContainer<Apple> appleComparable = new MyComparableContainer<Apple>(f1);
31 MyComplicatedContainer<Orange, Sunkist> orangeContainer = new MyComplicatedContainer<Orange, Sunkist>(f2, f3);
32 }
33 }
34
35
36
37
38
39
40 @author
41 @param <T>
42
43 class MySimpleContainer<T> {
44
45 private T data;
46
47 public MySimpleContainer(T data) {
48 this.setData(data);
49 }
50
51
52 @return
53
54 public T getData() {
55 return this.data;
56 }
57
58
59 @param
60
61 public void setData(T data) {
62 this.data = data;
63 }
64 }
65
66
67
68
69
70
71 @author
72 @param <T>
73
74
75 class MyComparableContainer<T extends Comparable<? super T>> implements Comparable<T> {
76
77 private T data;
78
79 public MyComparableContainer(T data) {
80 this.data = data;
81 }
82
83
84 @return
85
86 public T getData() {
87 return this.data;
88 }
89
90
91 @param
92
93 public void setData(T data) {
94 this.data = data;
95 }
96
97 @Override
98 public int compareTo(T other) {
99 return this.data.compareTo(other);
100 }
101 }
102
103
104
105
106
107 @author
108 @param <T>
109
110 @param <U>
111
112
113 class MyComplicatedContainer<T extends Comparable<? super T>, U extends T> extends MyComparableContainer<T> {
114
115 private U localData;
116
117 public MyComplicatedContainer(T parm1, U parm2) {
118 super(parm1);
119 this.localData = parm2;
120 }
121
122
123 @return
124
125 public U getLocalData() {
126 return localData;
127 }
128
129
130 @param
131
132 public void setLocalData(U localData) {
133 this.localData = localData;
134 }
135
136 }
137