forked from hacker85/JavaLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenBeginLesson.java
More file actions
51 lines (40 loc) · 1.03 KB
/
GenBeginLesson.java
File metadata and controls
51 lines (40 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package generics;
import java.util.ArrayList;
import java.util.List;
public class GenBeginLesson {
public static void main(String[] args) {
List list = new ArrayList();
// List<Integer> list = new ArrayList<>();
list.add(1);
list.add("bla");
for (int i = 0; i < list.size(); i++) {
int j = (int)list.get(i);
System.out.println(j);
}
NonGenericCell nonGenericCell = new NonGenericCell();
nonGenericCell.setItem(new Money());
Money money = (Money)nonGenericCell.getItem();
GenericCell<Money> cell = new GenericCell<>();
cell.setItem(new Money());
Money money2 = cell.getItem();
}
}
class Money {}
class NonGenericCell {
Object item;
public Object getItem() {
return item;
}
public void setItem(Object item) {
this.item = item;
}
}
class GenericCell<T> {
T item;
public T getItem() {
return item;
}
public void setItem(T item) {
this.item = item;
}
}