简体   繁体   中英

Having Problems with override the toString

The main problem I am having is the override the toString() and what exactly to put. If anyone can help that would be great!

// 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();
    }

This is the main problem I have with the code. I need help with how the toString() method works and what to put in!

    /**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() {

    }
}

Where is the difficult?

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

If color is private (in super class) you should use something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM