简体   繁体   中英

I can't figure out what's wrong, getting a 'type already defined error'

Ok so, I'm learning java using a book I bought online, and for some reason Java wont allow this program to work even though it's the exact text from the book. Can someone explain why I keep getting told 'The type SimpleCircle is already defined'? it shows this as an error next to the line "SimpleCircle() { radius = 1; }"

public class SimpleCircle {
    /** Main method */
    public static void main(String[] args) {
        //create a circle with radius 1
        SimpleCircle circle1 = new SimpleCircle();
        System.out.println("The area of the circle of radius " 
                + circle1.radius + " is " + circle1.getArea());

        //create a a circle with radius 25
        SimpleCircle circle2 = new SimpleCircle(25);
        System.out.println("The area of the circle of radius " 
                + circle2.radius + " is " + circle2.getArea());

        //create a circle with radius 125
        SimpleCircle circle3 = new SimpleCircle(125);
        System.out.println("the area of the circle of radius " 
                + circle3.radius + " is " + circle3.getArea());

        //modify circle radius
        circle2.radius = 100;
        System.out.println("The area of the circle of radius " 
                + circle2.radius + " is " + circle2.getArea());
    }

    double radius;

    /** construct a circle with radius 1 */
    SimpleCircle() {
        radius = 1;
    }

    /** construct a circle with a specified radius */
    SimpleCircle(double newRadius) {
        radius = newRadius;
    }

    // return the area of this circle
    double getArea() {
        return radius * radius * Math.PI;
    }

    // return the perimeter of the circle
    double getPerimeter() {
        return 2 * radius * Math.PI;
    }

    // set a new radius for this circle
    void setRadius(double newRadius) {
        radius = newRadius;
    }
}

我也遇到了同样的错误,这是因为您正在使用的文件夹包含在另一个使用相同类(此处为SimpleCircle)的文件(当前工作文件除外)中,只需修改类名(例如:当前文件中的SimpleCircle到SimpleCircle2),则错误将消失。

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