Bonjour à tous,

Je voudrais approfondir la discussion suivante que j'ai commencé fin mars :
http://www.developpez.net/forums/d15...per-generique/.

Maintenant j'ai une 4ième classe héritant de la classe A.

Petit rappel:
  • 1 classe racine R
  • 2 classes A et B héritant de R
  • 1 classe Z héritant de A (nouveauté)


Mais oublions B pour ne s'intéresser que la hiérarchie sur 3 niveaux : Z -> A -> R

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
package test.heritage2.model;
 
public class A extends R {
	private String ma1;
	private String ma2;
	private String ma3;
 
	@Override
	public String doSomething(String arg) {
		return "Class A (" + arg + ")";
	}
 
	public A loadFromA(A a) {
		ma1 = a.ma1;
		ma2 = a.ma2;
		ma3 = a.ma3;
		return this;
	}
 
	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 + ", " + super.toString() + "]";
	}
}
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
package test.heritage2.model;
 
public class R {
	private String mr1;
	private String mr2;
	private String mr3;
 
	public String doSomething(String arg) {
		return "Class R (" + arg + ")";
	}
 
	public R loadFromR(R r) {
		mr1 = r.mr1;
		mr2 = r.mr2;
		mr3 = r.mr3;
		return this;
	}
 
	public String getMr1() {
		return mr1;
	}
 
	public void setMr1(String mr1) {
		this.mr1 = mr1;
	}
 
	public String getMr2() {
		return mr2;
	}
 
	public void setMr2(String mr2) {
		this.mr2 = mr2;
	}
 
	public String getMr3() {
		return mr3;
	}
 
	public void setMr3(String mr3) {
		this.mr3 = mr3;
	}
 
	@Override
	public String toString() {
		return "R [mr1=" + mr1 + ", mr2=" + mr2 + ", mr3=" + mr3 + "]";
	}
}
La petite nouvelle

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
package test.heritage2.model;
 
public class Z extends A {
	private String mz1;
	private String mz2;
	private String mz3;
 
	@Override
	public String doSomething(String arg) {
		return "Class Z (" + arg + ")";
	}
 
	public String getMz1() {
		return mz1;
	}
 
	public void setMz1(String mz1) {
		this.mz1 = mz1;
	}
 
	public String getMz2() {
		return mz2;
	}
 
	public void setMz2(String mz2) {
		this.mz2 = mz2;
	}
 
	public String getMz3() {
		return mz3;
	}
 
	public void setMz3(String mz3) {
		this.mz3 = mz3;
	}
 
	@Override
	public String toString() {
		return "Z [mz1=" + mz1 + ", mz2=" + mz2 + ", mz3=" + mz3 + ", " + super.toString() + "]";
	}
}
Et le mapping, qui correspond à la solution simple proposée par tchize_ et que j'ai adoptée.

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
package test.heritage2;
 
import test.heritage2.model.A;
import test.heritage2.model.Z;
import test.heritage2.model.R;
 
public class App {
 
	public static void main(String[] args) {
		R instR = createR();
		System.out.println(instR.toString());
 
		A instA = ceateA(instR);
		System.out.println(instA.toString());
 
		Z instZ = ceateZ(instA);
		System.out.println(instZ.toString());
 
		String str = instZ.doSomething("pipo des alpes");
		System.out.println(str);
	}
 
	private static R createR() {
		R instR = new R();
		instR.setMr1("rrr1");
		instR.setMr2("rrr2");
		instR.setMr3("rrr3");
		return instR;
	}
 
	private static A ceateA(R instR) {
		A instA = new A();
		instA.setMa1("aaa1");
		instA.setMa2("aaa2");
		instA.setMa3("aaa3");
 
		instA = (A) instA.loadFromR(instR);
		return instA;
	}
 
	private static Z ceateZ(A instA) {
		Z instZ = new Z();
		instZ.setMz1("zzz1");
		instZ.setMz2("zzz2");
		instZ.setMz3("zzz3");
 
		instZ = (Z) instZ.loadFromA(instA);
		return instZ;
	}
 
}
Mais je n'obtiens pas cette fois-ci le résultat que j'attends.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
R [mr1=rrr1, mr2=rrr2, mr3=rrr3]
A [ma1=aaa1, ma2=aaa2, ma3=aaa3, R [mr1=rrr1, mr2=rrr2, mr3=rrr3]]
Z [mz1=zzz1, mz2=zzz2, mz3=zzz3, A [ma1=aaa1, ma2=aaa2, ma3=aaa3, R [mr1=null, mr2=null, mr3=null]]]
Class Z (pipo des alpes)
Puisque l'instance de la classe Z a bien hérité de l'instance de la classe A, mais pas de l'instance de la classe R.

Est-ce que cette solution est toujours applicable ?

Comment solutionner ce problème tout en gardant une solution simple et propre ?

Merci.