Généricité sur les méthodes
Bonjour,
Je suis en présence de code que je cherche à simplifier (c'est un « test case »).
J'ai les 2 classes suivantes A et B :
Code:
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
| public class A {
private String ma1;
private String ma2;
private String ma3;
public String getMa1() {
return ma1;
}
public void setMa1(String ma1) {
this.ma1 = ma1;
}
public String getMa2() {
return ma2;
}
public void setMa2(String ma2) {
this.ma2 = ma2;
}
public String getMa3() {
return ma3;
}
public void setMa3(String ma3) {
this.ma3 = ma3;
}
@Override
public String toString() {
return "A [ma1=" + ma1 + ", ma2=" + ma2 + ", ma3=" + ma3 + "]";
}
} |
Code:
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
| public class B {
private String mb1;
private String mb2;
private String mb3;
public String getMb1() {
return mb1;
}
public void setMb1(String mb1) {
this.mb1 = mb1;
}
public String getMb2() {
return mb2;
}
public void setMb2(String mb2) {
this.mb2 = mb2;
}
public String getMb3() {
return mb3;
}
public void setMb3(String mb3) {
this.mb3 = mb3;
}
@Override
public String toString() {
return "B [mb1=" + mb1 + ", mb2=" + mb2 + ", mb3=" + mb3 + "]";
}
} |
Et voici schématisé le programme principale :
Code:
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
| public class App1 {
public static void main(String[] args) {
A a = new A();
a.setMa1("aaa111");
a.setMa2("aaa222");
a.setMa3("aaa333");
B b = new B();
b.setMb1("bbb111");
b.setMb2("bbb222");
b.setMb3("bbb333");
toto(a);
toto(b);
}
private static <T> void toto(T obj) {
if (obj instanceof A) {
System.out.println("lulu: " + lulu((A) obj));
}
if (obj instanceof B) {
System.out.println("lulu: " + lulu((B) obj));
}
System.out.println("class: " + obj.getClass().getName());
System.out.println("value: " + obj.toString());
}
private static String lulu(A a) {
return a.getMa1();
}
private static String lulu(B b) {
return b.getMb2();
}
} |
qui me retourne cela:
Code:
1 2 3 4 5 6
| lulu: aaa111
class: domiq.generics.A
value: A [ma1=aaa111, ma2=aaa222, ma3=aaa333]
lulu: bbb222
class: domiq.generics.B
value: B [mb1=bbb111, mb2=bbb222, mb3=bbb333] |
Ce que je veux faire, c'est de rendre générique la méthode lulu().
J'ai pensé à ça mais Eclipse me retourne une erreur car cela doit être faux.
Code:
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
| public class App2 {
public static void main(String[] args) {
A a = new A();
a.setMa1("aaa111");
a.setMa2("aaa222");
a.setMa3("aaa333");
B b = new B();
b.setMb1("bbb111");
b.setMb2("bbb222");
b.setMb3("bbb333");
toto(a);
toto(b);
}
private static <T> void toto(T obj) {
System.out.println("lulu: " + lulu(obj)); // <== ERREUR !!! ???
System.out.println("class: " + obj.getClass().getName());
System.out.println("value: " + obj.toString());
}
private static <T extends A> String lulu(T a) {
return a.getMa1();
}
private static <T extends B> String lulu(T b) {
return b.getMb2();
}
} |
L'erreur est la suivante :
Code:
1 2
| Bound mismatch: The generic method lulu(T) of type App2 is not applicable for the arguments (T). The inferred type T
is not a valid substitute for the bounded parameter <T extends A> |
Est-ce que quelqu'un peut m'aider ?
Merci.