我們正在學習如何在Java
使用多個類,現在,有一個項目,詢問有關創建類Circle
其中將包含一個radius
和diameter
,然後引用它從主類找到直徑。此代碼繼續收到錯誤(在標題中提到的)Java錯誤 - 「無效的方法聲明,要求返回類型」
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通常很有用,它返回的錯誤信息非常清晰。下一次你有其中一個有短暫的休息時間,答案就會發生在你:) – mbatchkarov
'CircleR'不是'Circle'的構造函數。名稱*必須匹配。 – 2011-09-17 01:17:13