有问题覆盖toString

时间:2016-05-07 22:23:46

标签: java syntax 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();
    }

这是我对代码的主要问题。我需要帮助toString()方法如何工作以及放入什么内容!

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

    }
}

1 个答案:

答案 0 :(得分:2)

困难在哪里?

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

如果颜色是私人的(超级类),你应该使用这样的东西:

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