所以,我非常接近完成一个项目;但是,我正在尝试访问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()未定义
关于我做错了什么的任何帮助或想法。谢谢!
答案 0 :(得分:2)
注意到这一点:printCircle()
是类Circle
的函数,因此要访问此方法,您需要在对象上调用printCircle():
private static void showObjects(ArrayList<GeometricObject> list) {
for(GeometricObject o : list) {
if(o.equals("Circle"))
(Circle) o.printCircle();
}
}
试一试
答案 1 :(得分:0)
当您尝试拨打-
时,有一些不同的错误。主要问题是编译器尝试告诉您的问题,即您在printCircle
类中调用printCircle
。这没有意义;该方法属于hw2redo
类。这就是我修复代码的方法。判断你只关心Circle
当它是一个圆圈的事实,为什么甚至将它作为你的参数呢?将其更改为:
GeometricObject