0

我正在做一些家庭作业,无法弄清楚如何从 Point、Square 和 Cube 类中调用 toString 方法来打印出来。我知道这一定是一些愚蠢的事情,我想我只是敬酒,我的思想已经花光了。现在我有???在 toSting 方法应该去的地方。尝试了我能想到的所有组合(“class”.toString 等)。谁能告诉我我在哪里搞砸了?谢谢!!

import javax.swing.JOptionPane;

public class InheritanceTest {

public static void main(String args[]){
    // Declare variables
    String xString = null;
    String yString = null;
    String sideString = null;
    int x = 0;
    int y = 0;
    int sideLength = 0;

    try{
        xString = JOptionPane.showInputDialog("Enter x coordinate:");
        x = Integer.parseInt(xString);
        yString = JOptionPane.showInputDialog("Enter y coordinate:");
        y = Integer.parseInt(yString);
        sideString = JOptionPane.showInputDialog("Enter side of Square:");
        sideLength = Integer.parseInt(sideString);
        } // End try

        catch(NumberFormatException e){
            JOptionPane.showMessageDialog(  null,"The value you entered is not a valid number. Please try again",
                    "Error", JOptionPane.ERROR_MESSAGE);
        } // End catch

        JOptionPane.showMessageDialog(null, "Point: \n" +????????, "Results", JOptionPane.INFORMATION_MESSAGE);
} // End main           

}// End Class

class Point{

//Declare variables
private int x;
private int y;

// Point constructor
Point(int xCoordinate,int yCoordinate){
x = xCoordinate;
y = yCoordinate;
}// End Point Constructor

// Accessor to return x coordinate
public int getX(){
    return x;
}// End getX method

// Accessor to return y coordinate
public int getY(){
    return y;
}// End getY method

// Format and display coordinates
public String toString(){
    return "Corner = [" + x + "," + y + "]\n";
}// End toString

}// End Point Class

abstract class Square extends Point{

//Declare variables
private double sideLength;

// Square constructor
Square(int x, int y, double s){
    super(x, y);
    sideLength = s;
} // End Square constructor

// Accessor to return side length
public double getSide(){
    return sideLength;
} // End getSide

// Method to calculate area of square
public double area(){
    return sideLength * sideLength;
} // End area method 

// Method to calculate perimeter of square
public double perimeter(){
    return 4 * sideLength;
} // End perimeter method

// Format and display the square
public String toString(){
    return super.toString() + "Side length is: " + sideLength + "\n" +
                "Area is: " + area() + "\n" + "Perimeter is: " + perimeter();
} // End toString
}// End Square Class

abstract class Cube extends Square{

// Declare variable
double depth;

// Cube constructor
public Cube(int x, int y, double s, double z){
super(x, y, s);
depth = z;
} // End Cube constructor

// Method to calculate area of cube
public double area(){
    return 6 * super.area();
} // End Area

// Method  to calculate volume of cube
public double volume(){
    return super.area() * depth;
} // End volume

// Format and display the cube
public String toString(){
    return "Depth is: " + depth + "\n" + "Area is: " + area() + "\n" + "Volume is: " + volume();
} // End toString
} // End Cube class
4

4 回答 4

2

好吧,您需要先创建一个要使用的类的实例,例如:

Square s = new Square(10, 10, 2.0);
System.out.println(s.toString());
于 2013-09-23T02:39:38.480 回答
2

您不会从一个类调用 toString() 方法,而是从一个对象(即该类的一个实例)调用它。这obj.toString()objPoint、Square 或 Cube(或任何其他类)的实例。

不过,我没有看到您实例化这些类的任何对象。您需要先创建该类的实例,然后才能调用基于实例的方法,例如 toString()。

Point myPoint = new Point(x, y);
String pointString = myPoint.toString();
于 2013-09-23T02:43:12.900 回答
1
new Point(x,y).toString()

toString() 是 Point 类的一个方法。您需要先用 x 和 y 坐标实例化它,然后调用它的 toString() 方法。

于 2013-09-23T02:42:43.877 回答
0
JOptionPane.showMessageDialog(null, 
"Point: \n" + new Point(x, y).toString, 
"Results", 
JOptionPane.INFORMATION_MESSAGE);
于 2013-09-23T02:52:40.217 回答