-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
78 lines (65 loc) · 2.12 KB
/
Copy pathApp.java
File metadata and controls
78 lines (65 loc) · 2.12 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package devcode.module_06x03_wildcard;
import java.util.ArrayList;
import java.util.List;
public class App {
// Java Generics Unbounded Wildcard
public static void mostrarElementosUnbounded(List<? /*extends Object*/> elements){
System.out.println("{***Unbounded method***}");
for(Object obj : elements){
if(obj instanceof Perro) {
System.out.println(((Perro)obj).getTipo());
}else if(obj instanceof Gato){
System.out.println(((Gato)obj).getTipo());
}
}
}
// Java Generics Upper Bounded Wildcard
public static void mostrarElementosUpperBounded(List<? extends Animal> elements) {
System.out.println("{***Upper Bounded method***}");
for (Animal ani : elements) {
System.out.println(ani.getTipo());
}
}
//
// Java Generics Lower Wildcard
public static void mostrarElementosLowerBounded(List<? super Perro> elements){
System.out.println("{***Lower Bounded method***}");
for (Object obj : elements) {
System.out.println(((Animal)obj).getTipo());
}
}
public static void main(String[] args) {
/** Codigo un bounded **/
Perro p = new Perro("Perro Dalmata");
Perro p1 = new Perro("Pastor Aleman");
List<Perro> listA = new ArrayList<>();
listA.add(p);
listA.add(p1);
// Gato g1= new Gato("Gato siames");
// List<Gato> listA = new ArrayList<>();
// listA.add(g1);
mostrarElementosUnbounded(listA);
/** __________________________ **/
/** Codigo upper bounded **/
Animal b1 = new Perro("Perro Dalmata");
Perro b2 = new Perro("Pastor Aleman");
Animal b3= new Gato("Gato siames");
List<Animal> listB = new ArrayList<>();
listB.add(b1);
listB.add(b2);
listB.add(b3);
mostrarElementosUpperBounded(listB);
// /** __________________________ **/
/** Codigo lower bounded **/
Animal c1 = new Perro("Perro Dalmata");
Perro c2 = new Perro("Pastor Aleman");
Animal c3= new Gato("Gato siames");
Gato c4= new Gato("Gato persa");
List<Animal> listC = new ArrayList<>();
listC.add(c1);
listC.add(c2);
listC.add(c3);
listC.add(c4);
mostrarElementosLowerBounded(listC);
}
}