Salut,
Je viens de tomber sur un truc un peu bizarre en java. Bizarre ou en tout cas que j'avais pas prévu dans mon design.
En gros je veux créer une méthode doIt( Iterable<CSVwritable> )
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package testiterator; import java.util.Iterator; public class Main { public static void main(String[] args) { new Ecrivain1Ligne().doIt( new Ligne() ); // Compile new Ecrivain().doIt( new Liste() ); // Compile pas } } interface CSVwritable { public String toRowCSV(); } class Ligne implements CSVwritable { public String toRowCSV() { return "Test" ; } } class Liste implements Iterable<Ligne> { public Iterator<Ligne> iterator() { return new Iterator<Ligne>() { public boolean hasNext() { throw new UnsupportedOperationException("Not supported yet."); } public Ligne next() { throw new UnsupportedOperationException("Not supported yet."); } public void remove() { throw new UnsupportedOperationException("Not supported yet."); } }; } } class Ecrivain { void doIt( Iterable<CSVwritable> c ) { // void } } class Ecrivain1Ligne { void doIt( CSVwritable c ) { // void } }
Les objets Ligne sont de type CSVwritable. Je créer un objet de type Iterable<Ligne> mais si j'essaye de le passer à mon doIt(), ça ne passe pas, j'ai une erreur à la compilation. Pourtant les Ligne sont bien des CSVwritable, ce que je vérifie avec l'autre doIt().
De quoi suis-je passé à côté ? Comment faire ce que je veux ?
Partager