[英]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. 好的,我正在使用从网上购买的书来学习Java,由于某种原因,Java不允许该程序正常工作,即使它是书中的确切内容。 Can someone explain why I keep getting told 'The type SimpleCircle is already defined'? 有人可以解释为什么我不断被告知“类型SimpleCircle已经定义”吗? it shows this as an error next to the line "SimpleCircle() { radius = 1; }" 它在“ 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),则错误将消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.