【发布时间】:2015-10-13 02:09:40
【问题描述】:
所以,我非常接近完成一个项目;但是,我正在尝试访问 Circle 和 Rectangle 类中的方法。到目前为止,我的代码是:
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"))
printCircle();
}
}
}
class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
private String data;
/** Construct a default geometric object */
public GeometricObject() {
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) {
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 "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
}
class Circle extends GeometricObject
{
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public Circle(double radius,
String color, boolean 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() {
}
public Rectangle(
double width, double height) {
this.width = width;
this.height = height;
}
public Rectangle(
double width, double height, String color, boolean 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);
}
}
当我尝试访问我的 Circle 子类中的 printCircle 方法时,我收到了错误
没有为类型 hw2redo 定义方法 printCircle()
关于我做错了什么的任何帮助或想法。谢谢!
【问题讨论】:
-
究竟是哪一行给了你这个错误?
-
抱歉,肯定应该对代码进行处理。它在我的 showObjects 方法中(第三种方法)。 @迈克尔
-
printCircle()是Circle上的方法,而不是GeometricObject。更新这一行:for(GeometricObject o : list). -
我唯一能想到的是为每个循环制作两个:一个使用 Circle,另一个使用 Rectangle 而不是 GeometricObject 但它说我无法从元素类型 GeometricObject 转换为 Circle 但是我很困惑,因为我的 Circle 类扩展了 GeometricObject? @Eraph
标签: java subclass superclass dynamic-arrays