java - 正确使用toString

标签 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

最佳答案

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

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

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

关于java - 正确使用toString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18950985/

相关文章:

java - 如何使用java从Gitlab帐户下载文件

java - Java 中的 MySql 表监听器

java - 如何在不编译根项目的情况下编译maven项目?

java - 等同于 Java 中的 C++ map.lower_bound

java - 使用动态代理和 java 泛型进行类型转换静默失败

java - 从java文件中随机读取一个字符?

java - 默认 switch case 之后的 if 语句 - Java

Java - Eclipse - 从文件中获取名称和数字,按字母顺序排序到新文件中,按数字排序到新文件中

java - Glassfish 无法连接到 mysql 数据库

java - 使用实例变量 java 的多线程