Note that there are some explanatory texts on larger screens.

plurals
  1. POCall an method of a class in an array - Java
    primarykey
    data
    text
    <p>I am having a problem with a project.</p> <p>So basically what I have is a class Shape with some sub-class ( ShapeRectangle, ShapeTriangle, etc ). In each sub-class, I got an outputShape method: g.fillRect(getX(), getY(), getWidth(), getHeight());</p> <p>In another class, showShapes, I got an array that contains the sub-classes.</p> <p>I would like to run the method via the array.</p> <p>Is there any way to do it?</p> <p>EDIT: The array in showShapes is a Shape[] array.</p> <p>Here are the code of ShapeRect ( sorry, bits are in french ): import java.awt.Color; import java.awt.Graphics;</p> <pre><code>public class FormeRectangulaire extends Forme { public FormeRectangulaire(int x, int y, int width, int height){ setX(x); setY(y); setWidth(width); setHeight(height); setColor(Color.RED); } public void afficherForme(Graphics g){ g.setColor(getColor()); g.fillRect(getX(), getY(), getWidth(), getHeight()); } } </code></pre> <p>Here is the shape: import java.awt.Color; import java.awt.Graphics;</p> <pre><code>public class Forme { private int x; private int y; private int width; private int height; private Color color; /*public void afficherForme(Graphics g){ afficherForme(g); }*/ public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public Color getColor(){ return color; } public void setColor(Color color){ this.color = color; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } } </code></pre> <p>And here is how I put each class inside the array:</p> <pre><code> public void creerArrayFormes(){ String[][] donnes = getDatas(); if(donnes[getDatasElement()][0].equals("CARRE") || donnes[getDatasElement()][0].equals("RECTANGLE")){ FormeRectangulaire rect = new FormeRectangulaire(Integer.parseInt(donnes[getDatasElement()][1]),Integer.parseInt(donnes[getDatasElement()][2]),Integer.parseInt(donnes[getDatasElement()][3]),Integer.parseInt(donnes[getDatasElement()][4])); setFormes(rect, getDatasElement()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload