关于eclipse:Java toString不起作用

Java toString doesn't work

本问题已经有最佳答案,请猛点这里访问。

我有一个应用程序在eclipse的rectangel count,高度,宽度从点(P1、P2、P3,…,P10)。

印刷的点是这样的:

1
Point@659e0bfd, Point@2a139a55, Point@15db9742, Point@6d06d69c, Point@7852e922, Point@4e25154f, Point@70dea4e, Point@5c647e05, Point@33909752, Point@55f96302

你能告诉我它看起来应该如何样?I don’t get it toString方法的论文。在其余的代码是什么样子的?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    import java.util.ArrayList;
import java.util.List;

public class App {      //new public class App


    public static void main(String[] args) {

        Point p1 = new Point(10.681,-48.857);   // new Points (x,y)
        Point p2 = new Point(96.980,20.724);
        Point p3 = new Point(66.647,-66.558);
        Point p4 = new Point(-2.674,-58.571);
        Point p5 = new Point(40.11,-12.342);
        Point p6 = new Point(27.782,46.809);
        Point p7 = new Point(54.759,-46.709);
        Point p8 = new Point(-33.89,-90.787);
        Point p9 = new Point(15.84,-67.553);
        Point p10 = new Point(19.481,51.331);


        List<Point> list1 = new ArrayList<Point>(); // Create ArrayList and add Points
        list1.add(p1);
        list1.add(p2);
        list1.add(p3);
        list1.add(p4);
        list1.add(p5);
        list1.add(p6);
        list1.add(p7);
        list1.add(p8);
        list1.add(p9);
        list1.add(p10);

        public String toString() {
            return Point;

        }

        new BoundingBox(list1);
        double c = BoundingBox.height();
        double d = BoundingBox.width();

        System.out.println("Das Array beinhaltet die Punkte" + list1 ); // This prints:"Das Array beinhaltet die Punkte" and the list
        System.out.println("Die BoundingBox hat die Hohe" + c +" und die Breite" + d +"
"
);   // This prints the height and the width
        System.out.println("Der maximale Punkt der BBox betragt (" + BoundingBox.getMaxPoint() +")." ); // This prints:
        System.out.println("Der minimale Punkt der BBox betragt (" + BoundingBox.getMinPoint() +")." ); // This prints:


    }
}

边界框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.List;

public class BoundingBox {      //new class BoundingBox
private static Point minPoint;      //new
private static Point maxPoint;


public BoundingBox(List<Point> manyPoints) {
    double minX = manyPoints.get(0).getX();
    double maxX = manyPoints.get(0).getX();
    double minY = manyPoints.get(0).getY();
    double maxY = manyPoints.get(0).getY();

    for (int i = 0; i < manyPoints.size(); i++) {
        Point test = manyPoints.get(i);
        test.getX();

        if (test.getX() < minX) {
            minX = test.getX();    

        }

        if (test.getX() > maxX) {
            maxX = test.getX();
        }

        if (test.getY() < minY) {
            minY = test.getY();

        }

        if (test.getY() > maxY) {
            maxY = test.getY();

        }

        minPoint = new Point(minX, minY);
        maxPoint = new Point(maxX, maxY);

    }
}

public static double width() {
    double a = (maxPoint.getX() - minPoint.getX());     // calculate the width
    return a;
}

public static double height() {
    double b = (maxPoint.getY() - minPoint.getY());     // calculate the width
    return b;
}

public static Point getMaxPoint() {
    return maxPoint;
}

public static Point getMinPoint() {
    return minPoint;
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Point {


private double x;  
private double y;

public Point(double xnew, double ynew) {
    x = xnew;
    y = ynew;
}

public double getX() {  
    return x;
}

public void setX(double xnew) {
    x = xnew;
}

public double getY() {
    return y;
}

public void setY(double ynew) {
    y = ynew;
}




}


您应该在点类中重写toString

1
2
3
4
5
6
7
8
9
public class Point {
    ...
    @Override
    public String toString ()
    {
        return x +"," + y; // or whatever you wish to display
    }
    ...
}

哦,在你的App课上你打算做什么还不清楚:

1
2
3
4
    public String toString() {
        return Point;

    }

这甚至不应该通过编译。而且,除非您试图打印一个App实例,否则不必覆盖ApptoString


根据文档,如果您没有指定自己的toString方法的override,则会调用父方法(在本例中,是Object方法),这会导致:

1
 getClass().getName() + '@' + Integer.toHexString(hashCode())


在您的Point类中添加以下方法

1
2
3
public String toString() {
  return"Point[" + x +"," + y +"]";
}