【发布时间】:2011-09-17 00:47:55
【问题描述】:
我们现在正在学习如何在Java 中使用多个类,并且有一个项目要求创建一个类Circle,其中将包含一个radius 和一个diameter,然后从主类中引用它找到直径。此代码继续收到错误(标题中提到)
public class Circle
{
public CircleR(double r)
{
radius = r;
}
public diameter()
{
double d = radius * 2;
return d;
}
}
感谢您的帮助,-AJ
更新 1:
好的,但我不应该将第三行 public CircleR(double r) 声明为双精度,对吧?在我正在学习的书中,示例并没有这样做。
public class Circle
{
//This part is called the constructor and lets us specify the radius of a
//particular circle.
public Circle(double r)
{
radius = r;
}
//This is a method. It performs some action (in this case it calculates the
//area of the circle and returns it.
public double area( ) //area method
{
double a = Math.PI * radius * radius;
return a;
}
public double circumference( ) //circumference method
{
double c = 2 * Math.PI * radius;
return c;
}
public double radius; //This is a State Variable…also called Instance
//Field and Data Member. It is available to code
// in ALL the methods in this class.
}
如您所见,代码public Circle(double r).... 与我在public CircleR(double r) 中所做的有什么不同?无论出于何种原因,书中的代码都没有给出错误,但是我说那里有错误。
【问题讨论】:
-
Javac 通常很有帮助,它返回的错误信息也很清楚。下次你有其中一个的时候休息一下,答案会来找你的:)
-
CircleR不是Circle的构造函数。名称必须匹配。
标签: java