Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在数组中调用类的方法-Java_Java_Arrays_Class_Methods_Shape - Fatal编程技术网

在数组中调用类的方法-Java

在数组中调用类的方法-Java,java,arrays,class,methods,shape,Java,Arrays,Class,Methods,Shape,我对一个项目有问题 所以基本上我有一个类形状和一些子类(ShapeRectangle,ShapeTriangle,等等)。 在每个子类中,我都有一个outputShape方法: g、 fillRect(getX(),getY(),getWidth(),getHeight()) 在另一个类showShapes中,我得到了一个包含子类的数组 我想通过数组运行这个方法 有什么办法吗 编辑: showShapes中的数组是Shape[]数组 以下是Shapper的代码(对不起,bits是法语): 导入j

我对一个项目有问题

所以基本上我有一个类形状和一些子类(ShapeRectangle,ShapeTriangle,等等)。 在每个子类中,我都有一个outputShape方法: g、 fillRect(getX(),getY(),getWidth(),getHeight())

在另一个类showShapes中,我得到了一个包含子类的数组

我想通过数组运行这个方法

有什么办法吗

编辑: showShapes中的数组是Shape[]数组

以下是Shapper的代码(对不起,bits是法语): 导入java.awt.Color; 导入java.awt.Graphics

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());
  }
}
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;
}

}
形状如下: 导入java.awt.Color; 导入java.awt.Graphics

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());
  }
}
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;
}

}
下面是我如何将每个类放入数组中的:

    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());
    }

}

必须创建数组对象或超类的
列表

List shapes=new ArrayList();
//列表形状=新建ArrayList();在jdk7中
添加(新的ShapeRectangle());
添加(新的ShapeTriangle());
//....
创建循环以获取对象:

  for(int i = 0; i<shapes.size();i++){
      Object obj = shapes.get(i);
      if(objinstanceof ShapeRectangle){
         ((ShapeRectangle)obj).fillRect(....);
      }
      else if(list.get(i)
    }

对于(int i=0;i我强烈建议将outputShape添加到Shape类中。如果Shape是自然抽象的(不期望实际创建新的Shape()),则它可以是抽象的

如果这样做,您只需迭代Shape[]并调用元素的outputShape。这将调用元素实际类的outputShape版本:

for(Shape s: shapes) {
  s.outputShape();
}

Shape是否将outputShape声明为抽象方法?如果不是,为什么不是?数组是否声明为Shape[]?如果我理解正确,不应该在数组中循环调用每个对象中的方法?数组声明为Shape[]。而且没有形状不会将outputShape声明为抽象方法。我不明白,我为什么要这样做。请提供一些代码片段。但问题是,创建此方法的全部目的是避免使用if和else并减少代码。我本可以制作一个简单的g.fillrect,根据形状使用一些off。你是说你有一种方法d那画矩形和三角形的
fillRect(…)
那画矩形和方法fillTri(…)在Shapper子类内部。好的,如果没有
if-else
您如何知道要绘制什么?或者您如何知道从列表中获取的对象是矩形或三角形?我已经有了if-else,它可以确定它是什么形状,并创建正确的子类,然后将其放置在数组中。因此,我想知道if-e的重用lse因为那只是重复不?我不明白你的意思。因为在那之后我需要将这些形状添加到JFrame…所以改变你对outputShape方法的操作,或者向它添加参数。我试图理解的关键思想是,你应该将所有子类都有的常用方法塑造成形状,然后EN不需要做一堆IFS或开关来运行正确的方法。@ AraSivaneswaran:也可以考虑使用<代码> java .AWT。