Ich suche weiter, wie dies zu beheben ist, aber ich kann nicht finden, was für mein Problem gilt/funktioniert. Ich versuche ein Array von Circles
auszudrucken. Alles was ich weiß ist, dass ich die toString()
Methode überschreiben muss.System.out.print (Arrays.toString()); funktioniert nicht für Array von OBJEKTEN
Ich halte die folgende Ausgabe bekommen:
[Heather $ Kreis @ 2a139a55, Heather $ Kreis @ 15db9742, Heather $ Kreis @ 6d06d69c, Heather $ Kreis @ 7852e922]
import java.util.*;
public class heather {
public static void main(String[] args) {
heather c = new heather();
Circle c1 = c.new Circle(4,6,4);
Circle c2 = c.new Circle(4,5,4);
Circle c3 = c.new Circle(5,4,4);
Circle c4 = c.new Circle(5,4,3);
Circle[] a = {c1, c2, c3, c4};
Arrays.sort(a);
System.out.print(Arrays.toString(a));
}
public class Point {
private int x;
private int y;
public Point(int x, int y){
this.x = x; this.y = y;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
}
public class Circle extends Point implements Comparable<Circle> {
private double radius;
private Point point;
public Circle(int x, int y, double radius) {
super(x, y); this.radius = radius;
}
public double getRadius() {
return this.radius;
}
public Point getPoint() {
return this.point;
}
public int area() {
return (int) (Math.PI*radius*radius);
}
public int compareTo(Circle other){
if(this.area()>other.area()) {
return 1;
}
if(this.area()<other.area()) {
return -1;
} else if(this.getX()>other.getX()) {
return 1;
}
if (this.getX()<other.getX()){
return -1;
} else if(this.getY()<other.getY()) {
return -1;
} else {
return 1;
}
}
}
public String toString(){
}
}
Sie müssen – 3kings
im 'toString' Methode füllen geschrieben gereinigt Ich weiß, es ausgefüllt werden muss. Aber ich weiß nicht, was nach innen zu setzen es. Ich habe versucht, getter Methoden und Sachen, aber ich bekomme immer rote Unterstreichungen. –
Umm so setzen vielleicht .... 'return" Radius: "+ Radius +" || X: "super.getX() +" || Y: "+ super.getY();' Es kommt wirklich darauf an, was Sie möchte, dass es ausgedruckt wird, wenn Sie es anordnen. – 3kings