正确使用toString

时间:2013-09-23 02:33:35

标签: java

我正在做一些家庭作业,无法弄清楚如何从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 个答案:

答案 0 :(得分:2)

您需要先创建要使用的类的实例,例如:

Square s = new Square(10, 10, 2.0);
System.out.println(s.toString());

答案 1 :(得分:2)

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

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

Point myPoint = new Point(x, y);
String pointString = myPoint.toString();

答案 2 :(得分:1)

new Point(x,y).toString()

toString()是Point类的一种方法。您需要首先使用x和y坐标对其进行实例化,然后调用其toString()方法。

答案 3 :(得分:0)

JOptionPane.showMessageDialog(null, 
"Point: \n" + new Point(x, y).toString, 
"Results", 
JOptionPane.INFORMATION_MESSAGE);