java - 使用 for 每个循环来打印对象

标签 java object arraylist foreach

所以我需要使用一种方法来打印矩形和圆形对象的内容。我希望它们的格式像这样

GeometricObject [color=red, filled=false, dateOfCreation=Wed Feb 11 12:21:51 EST
2015]
Circle [ radius= 4.0 Area=50.27 Perimeter=25.13 ]

我得到了方法签名,所以我必须使用它,我尝试了两种不同的方法来测试该对象是矩形还是圆形,然后采取正确的操作,但我无法让它们打印任何内容(我相信我可能缺少我的公共(public)类 hw2redo 中的某些内容,更具体地说是我的 recreateObject 方法)。我的目标逻辑是,如果该对象等于一个圆(在我的 recreateObject 方法中,我有两个圆或矩形的返回签名),那么它将使用 Circle 类中的 printCircle 方法,但没有输出。我的第二个逻辑路径是,如果对象列表中的任何位置包含“矩形”一词,当然这意味着它是一个矩形对象,并且将访问 矩形 类中的 printRectangle 方法,但同样什么也没有。但是,我会注意如果我只是使用 ((Circle) o).printCircle(); 例如,它将为我的所有圆形对象调用 printCircle 方法,并在到达我的第一个矩形对象时抛出异常(我想我已经接近了!)。

private static void showObjects(ArrayList<GeometricObject> list) {

         for(GeometricObject o : list) {

             if ((o.equals("Circle")))
             ((Circle) o).printCircle();
             if (o.equals(list.contains("Rectangle")))
             ((Rectangle) o).printRectangle();
         }
    }

如果您感兴趣,这是我的全部代码。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;


public class hw2redo 
{
     public static void main(String[] args) throws FileNotFoundException {

          GeometricObject g = null;
          File diskFile = new File("file.txt");
          Scanner diskScanner = new Scanner(diskFile);
          ArrayList<GeometricObject> list = new ArrayList<GeometricObject>();
          while(diskScanner.hasNext()){
              String geolist = diskScanner.nextLine();
              g = recreateObject(geolist);

              list.add(g);

          }
          diskScanner.close();
         /* while (diskScanner.hasNext()) {
              String list = diskScanner.nextLine();
              g = recreateObject(list);
          }
          diskScanner.close();*/
          showObjects(list);
       }





    private static GeometricObject recreateObject(String data) {

          String[] list = data.split(",");
          String geoObject = list[0];

          if (geoObject.equals("Circle")) {
             String color = list[1];
             boolean filled = Boolean.valueOf(list[2]); 
             double radius = Double.valueOf(list[3]);
             return new Circle(radius, color, filled);
          }

          if (geoObject.equals("Rectangle")) {
             String color = list[1];
             boolean filled = Boolean.valueOf(list[2]);
             double height = Double.valueOf(list[3]);
             double width = Double.valueOf(list[4]);
             return new Rectangle(width, height, color, filled);
          }
        return null;


       }

    private static void showObjects(ArrayList<GeometricObject> list) {

         for(GeometricObject o : list) {

             if ((o.equals("Circle")))
             ((Circle) o).printCircle();
             if (o.equals(list.contains("Rectangle")))
             ((Rectangle) o).printRectangle();
         }
    }
}

abstract class GeometricObject {
       private String color = "white";
       private boolean filled;
       private java.util.Date dateCreated;
       private String data;

      /** Construct a default geometric object */
      public GeometricObject() {
          super();
     dateCreated = new java.util.Date();
     //this.data = data;
      }


    /** Construct a geometric object with the specified color
     * and filled value */
     public GeometricObject(String color, boolean filled) {
     super();
     dateCreated = new java.util.Date();
     this.color = color;
     this.filled = filled;

     }

     /** Return color */
     public String getColor() {
     return color;
     }

     /** Set a new color */
     public void setColor(String color) {
     this.color = color;
     }

     /** Return filled. Since filled is boolean,
     its getter method is named isFilled */
     public boolean isFilled() {
     return filled;
     }

     /** Set a new filled */
     public void setFilled(boolean filled) {
     this.filled = filled;
     }

     /** Get dateCreated */
     public java.util.Date getDateCreated() {
     return dateCreated;
     }

     /** Return a string representation of this object */
     public String toString() {
     return  "GeometricObject [color=" + color +", filled="+ filled + ", dateOfCreation=" + dateCreated +
             "]\n";

     }
     }
class Circle extends GeometricObject
{
private double radius;

public Circle() {
    super();
    radius = 1;

}


public Circle(double radius,
String color, boolean filled) {
    super(color, filled);
this.radius = radius;
setColor(color);
setFilled(filled);
}

/** Return radius */
public double getRadius() {
return radius;
}

/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}

/** Return area */
public double getArea() {
return radius * radius * Math.PI;
}

/** Return diameter */
public double getDiameter() {
return 2 * radius;
}

/** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}

/** Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}


class Rectangle extends GeometricObject {
private double width;
private double height;

public Rectangle() {
    super();
}



public Rectangle(
double width, double height, String color, boolean filled) {
    super(color, filled);
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}

/** Return width */
public double getWidth() {
return width;
}

/** Set a new width */
public void setWidth(double width) {
this.width = width;
}

/** Return height */
public double getHeight() {
return height;
}

/** Set a new height */
public void setHeight(double height) {
this.height = height;
}

/** Return area */
public double getArea() {
return width * height;
}

/** Return perimeter */
public double getPerimeter() {
return 2 * (width + height);
}
public void printRectangle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + width);
}
}

我的 file.txt(我试图重新创建的对象)包含

Circle,green,false,4.0
Circle,blue,false,2.0
Circle,blue,true,7.0
Rectangle,orange,true,10.0,6.0
Rectangle,green,false,5.0,11.0
Rectangle,red,true,14.0,12.0

任何正确方向的帮助或提示将不胜感激。非常感谢!

最佳答案

我没有在您的代码中看到 .equals() 方法被重写,以便您可以使用 showObjects() 方法中的 if ((o.equals("Circle")))

您需要在代码中适当处理.equals()方法或者您可以使用

 if ( o instanceof Circle) 

而不是 showObjects() 方法中的 if ((o.equals("Circle"))) 。您可能也需要对其他对象(例如上面提到的矩形)执行相同的操作。

关于java - 使用 for 每个循环来打印对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33094249/

相关文章:

java - 找到n个最大的数字

没有命名空间的 xml 的 Java xsd 验证

java - java中如何传递变量

c# - 将对象转换为 List<object>

Java 编程和垃圾收集

java - 从给定索引处将 ArrayList 添加到另一个 ArrayList

java - 如何在 Java 中从 MySQL 检索查询的结果集

java - 如何以编程方式在第二个工作台中的模型上运行 Xpand 工作流程?

c++ - 通过显式调用构造函数(方法)构造对象

java - 帮助将 ArrayList 与我的结果集一起使用