2016-05-07 12 views
1

Das Hauptproblem, das ich habe, ist die toString() überschreiben und was genau zu setzen. Wenn jemand helfen kann, wäre das großartig!Probleme mit override toString

// Circle inherits from GeometricObject and uses Drawable 

public class Circle extends GeometricObject { 
    private double radius; 

    /**Default constructor*/ 
    public Circle() { 
     this(1.0); 
    } 

    /**Overloaded constructor - construct a circle with a specified radius*/ 
    public Circle(double radius) { 
     //call the local overloaded constructor and pass it the following: radius, "white", false 
     this.radius = radius; 
    } 

    /**Overloaded constructor - construct a circle with specified radius, filled, and color*/ 
    public Circle(double radius, String color, boolean filled) { 
     //call the GeometricObject's constructor and pass it color and filled 
     this.radius = radius; 
     setColor(color); 
     setFilled(filled); 
    } 

    /**Return radius*/ 
    public double getRadius() { 
     return radius; 
    } 

    /**Set a new radius*/ 
    public void setRadius(double radius) { 
     this.radius = radius; 
    } 

    /**Implement the getArea method defined in GeometricObject*/ 
    public double getArea() { 
     return radius*radius*Math.PI; 
    } 

    /**Implement the getPerimeter method defined in GeometricObject*/ 
    public double getPerimeter() { 
     return 2*radius*Math.PI; 
    } 

    /**Override the equals() method defined in the Object class*/ 
    public boolean equals(Circle circle) { 
     return this.radius == circle.getRadius(); 
    } 

Dies ist das Hauptproblem, das ich mit dem Code habe. Ich brauche Hilfe bei der Frage, wie die toString() Methode funktioniert und was Sie einsetzen können!

/**Override the toString() method defined in the Object class*/ 
    //output for circle should be: "[Circle] radius = ; color = ;    filled = " : insert appropriate variables to the right of the equal sign 
    @Override 
    public String toString() { 
     return [Circle] 
    } 

    @Override 
    public String Draw() { 

    } 

    @Override 
    public String howToDraw() { 

    } 
} 
+0

Warum ist dies markiert als 'C++'? Ich kann mir nicht vorstellen, dass du denkst, dass sie dieselbe Sprache sind. (Entfernen Sie das Tag) –

+0

Ich würde die IDE toString() -Methode verwenden, bis Sie entscheiden, dass Sie es anpassen möchten. –

+1

Okay, Sie haben es richtig, nur nicht das, was Sie zurückgeben .. tun Sie einfach etwas wie .. 'return '[Kreis] radius =" + radius + "; color =" + color + ";"; 'Sie benötigen eine Farbe Variable von einer Art wie 'String color' und Sachen, damit das funktioniert. – 3kings

Antwort

2

Wo ist das schwierig?

return "[Circle] radius: "+this.radius+" ; color = "+this.color+";"; 

Wenn Farbe ist privat (in Superklasse) Sie so etwas wie diese verwenden sollten:

return "[Circle] radius: "+this.radius+" ; color = "+getColor()+";"; 
-1

Hope this gewünschten Code.

@Override 
public String toString() 
{ 
    return "[Circle] radius = " + radius + "; color = " +color ;  
}