Bonjour à tous,
Je débute en java et je me retrouve face à un problème avec des classes abstracts et de l'héritage. Voici le code.
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
69
70 import java.awt.Point; import java.awt.Color; public class Element{ // --------------------------------------------------------- // Variables de classe // --------------------------------------------------------- public static final Color DEFAULT_ELEMENT_COLOUR = Color.GREEN; // --------------------------------------------------------- // Attributs // --------------------------------------------------------- private Point coordinate; private Color colour; // --------------------------------------------------------- // Constructeurs // --------------------------------------------------------- public Element(){ } public Element(Point coordinate){ this(coordinate,DEFAULT_ELEMENT_COLOUR); } public Element(int x, int y){ this(new Point(x,y),DEFAULT_ELEMENT_COLOUR); } public Element(Point coordinate, Color colour){ this.coordinate = coordinate; this.colour = colour; } public Element(int x, int y, Color colour){ this(new Point(x,y),colour); } // --------------------------------------------------------- // Méthodes // --------------------------------------------------------- public void setCoordinate(Point coordinate){ this.coordinate.setLocation(coordinate); } public void setCoordinate(int x, int y){ coordinate.setLocation(x,y); } public Point getCoordinate(){ return coordinate.getLocation(); } public void setX(int x){ coordinate.setLocation(x,this.getY()); } public void setY(int y){ coordinate.setLocation(this.getX(),y); } public int getX(){ return (int)coordinate.getX(); } public int getY(){ return (int)coordinate.getY(); } public void setColour(Color colour){ this.colour = colour; } public Color getColour(){ return colour; } public String toString(){ return "(" + (int)coordinate.getX() + "," + (int)coordinate.getY() + ")"; } }
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 import java.awt.Point; import java.awt.Color; import java.util.ArrayList; import java.util.List; abstract public class Piece{ // --------------------------------------------------------- // Attributs // --------------------------------------------------------- private List<Element> elements = new ArrayList<Element>(); // --------------------------------------------------------- // Constructeurs // --------------------------------------------------------- public Piece(){ } public Piece(Point coordinate){ this(coordinate,Element.DEFAULT_ELEMENT_COLOUR); } public Piece(int x, int y){ this(new Point(x,y),Element.DEFAULT_ELEMENT_COLOUR); } public Piece(Point coordinate, Color colour){ setElements(coordinate,colour); } public Piece(int x, int y, Color colour){ this(new Point(x,y),colour); } // --------------------------------------------------------- // Méthodes // --------------------------------------------------------- abstract protected void setElements(Point coordinate, Color colour); public List<Element> getElements(){ return elements; } public String toString(){ String result = getClass().getSimpleName(); for(int i=0;i<elements.size();i++){ result+= elements.get(i).toString(); result+= (i<elements.size()-1)?" , ":""; } return result; } }Le problème : Lors de la compilation de OPiece.java j'ai une erreur comme quoi elements est private. Je comprends l'erreur mais je ne sais pas comment faire pour corriger cela sans modifier la classe Piece.
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 import java.awt.Point; import java.awt.Color; import java.util.ArrayList; import java.util.List; public class OPiece extends Piece{ // --------------------------------------------------------- // Variables de classe // --------------------------------------------------------- private static final Point[] RELATIVE_ELEMENTS_COORDS = {new Point(0,0),new Point(0,-1),new Point(1,-1),new Point(1,0)}; // --------------------------------------------------------- // Constructeurs // --------------------------------------------------------- public OPiece(Point coordinate){ this(coordinate,Element.DEFAULT_ELEMENT_COLOUR); } public OPiece(int x, int y){ this(new Point(x,y),Element.DEFAULT_ELEMENT_COLOUR); } public OPiece(Point coordinate, Color colour){ super(coordinate,colour); } public OPiece(int x, int y, Color colour){ this(new Point(x,y),colour); } // --------------------------------------------------------- // Méthodes // --------------------------------------------------------- protected void setElements(Point coordinate, Color colour){ for(int i=0;i<RELATIVE_ELEMENTS_COORDS.length;i++){ int x = (int)coordinate.getX()+(int)RELATIVE_ELEMENTS_COORDS[i].getX(); int y = (int)coordinate.getY()+(int)RELATIVE_ELEMENTS_COORDS[i].getY(); elements.add(new Element(x,y,colour)); } } }
J'espère que vous pourrez m'aider.
Je vous remercie d'avance
Cordialement
Guillaume
Partager