【问题标题】:Can't access methods in my subclasses无法访问我的子类中的方法
【发布时间】: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


【解决方案1】:

请注意:printCircle()Circle 类的函数,因此要访问此方法,您需要在对象上调用 printCircle():

private static void showObjects(ArrayList<GeometricObject> list) {
     for(GeometricObject o : list) {
             if(o.equals("Circle"))
                 (Circle) o.printCircle();
     }
}

试试这个

【讨论】:

  • 啊!然而,铸造确实消除了错误。我没有收到任何输出?
  • 我建议在您的 recreateObject 方法中添加一些调试或打印语句,也许您正在阅读的文件导致此函数返回 null?
【解决方案2】:

当您尝试拨打printCircle 时,有一些不同的地方是错误的。主要问题,也是编译器试图告诉您的问题,是您在 hw2redo 类中调用 printCircle。那没有意义;该方法属于Circle 类。这就是我将如何修复您的代码。从你只关心GeometricObject 的事实来看,它是一个圆圈,为什么还要把它作为你的参数呢?将其更改为:

private static void showObjects(ArrayList<Circle> list){
    for(Circle c : list){
        c.printCircle();
    }
}

【讨论】:

  • 实际上,我需要对我的矩形对象做同样的事情(我刚刚决定我应该弄清楚如何正确地让圆工作)。请问为什么在我的 hw2redo 类中调用它是不正确的?因为我需要使用一种方法来调用我的矩形和圆形方法。一旦我弄清楚了 circle 方法的问题,我打算为矩形添加第二个 if 语句。
  • 因为printCircle 不在hw2redo 中,这是您调用它的地方。 @RiFFRAFF
  • 但是为什么我可以在我的 GeometricObjects 类中访问 toString 方法呢?
猜你喜欢
  • 2021-10-18
  • 1970-01-01
  • 2019-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-12
  • 1970-01-01
相关资源
最近更新 更多