简体   繁体   English

Java错误-“无效的方法声明; 需要返回类型”

[英]Java error - “invalid method declaration; return type required”

We are learning how to use multiple classes in Java now, and there is a project asking about creating a class Circle which will contain a radius and a diameter , then reference it from a main class to find the diameter. 我们现在正在学习如何在Java使用多个类,并且有一个项目在询问如何创建一个类Circle ,该类将包含一个radius和一个diameter ,然后从主类中引用它来找到直径。 This code continues to receive an error (mentioned in the title) 此代码继续收到错误(标题中提到)

public class Circle
{
    public CircleR(double r)
    {
        radius = r;
    }
    public diameter()
    {
        double d = radius * 2;
        return d;
    }
}

Thanks for any help, -AJ 感谢您的帮助,-AJ

Update 1 : Okay, but I shouldn't have to declare the third line public CircleR(double r) as a double, right? 更新1 :好的,但是我不必将第三行public CircleR(double r)为double,是吗? In the book I am learning from, the example doesn't do that. 在我正在学习的书中,该示例没有做到这一点。

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. 
     } 

As you can see, the code public Circle(double r).... how is that different from what I did in mine with public CircleR(double r) ? 如您所见,代码public Circle(double r)....与我在public CircleR(double r)所做的有什么不同? For whatever reason, no error is given in the code from the book, however mine says there is an error there. 无论出于什么原因,本书的代码中都没有错误,但是我的说法是那里有错误。

As you can see, the code public Circle(double r).... how is that different from what I did in mine with public CircleR(double r)? 如您所见,代码public Circle(double r)....与我在public CircleR(double r)中所做的有什么不同? For whatever reason, no error is given in the code from the book, however mine says there is an error there. 无论出于什么原因,本书的代码中都没有错误,但是我的说法是那里有错误。

When defining constructors of a class, they should have the same name as its class. 在定义类的构造函数时,它们应与该类具有相同的名称。 Thus the following code 因此下面的代码

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; 
  }
 ....
} 

is correct while your code 你的代码是正确的

public class Circle
{
    private double radius;
    public CircleR(double r)
    {
        radius = r;
    }
    public diameter()
    {
       double d = radius * 2;
       return d;
    }
}

is wrong because your constructor has different name from its class. 是错误的,因为构造函数的名称与其类不同。 You could either follow the same code from the book and change your constructor from 您可以遵循本书中的相同代码,并从

public CircleR(double r) 

to

public Circle(double r)

or (if you really wanted to name your constructor as CircleR) rename your class to CircleR. 或者(如果您确实想将构造函数命名为CircleR),则将您的类重命名为CircleR。

So your new class should be 所以你的新课应该是

public class CircleR
{
    private double radius;
    public CircleR(double r)
    {
        radius = r;
    }
    public double diameter()
    {
       double d = radius * 2;
       return d;
    }
}

I also added the return type double in your method as pointed out by Froyo and John B. 正如Froyo和John B所指出的,我还在您的方法中添加了返回类型double。

Refer to this article about constructors. 请参阅有关构造函数的本文

HTH. HTH。

每个方法(构造函数除外)都必须具有返回类型。

public double diameter(){...

You forgot to declare double as a return type 您忘记将double声明为返回类型

public double diameter()
{
    double d = radius * 2;
    return d;
}

I had a similar issue when adding a class to the main method. 将类添加到main方法时,我遇到了类似的问题。 Turns out it wasn't an issue, it was me not checking my spelling. 原来这不是问题,是我没有检查拼写。 So, as a noob, I learned that mis-spelling can and will mess things up. 因此,作为一个菜鸟,我了解到拼写错误可以并且会弄乱事情。 These posts helped me "see" my mistake and all is good now. 这些帖子帮助我“看到”了我的错误,现在一切都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM