Java错误 - “无效的方法声明;需要返回类型“ [英] Java error - "invalid method declaration; return type required"

查看:189
本文介绍了Java错误 - “无效的方法声明;需要返回类型“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在学习如何在 Java 中使用多个类,并且有一个项目要求创建一个类 Circle 将包含 radius 直径,然后从主类中引用它以查找直径。此代码继续收到错误(在标题中提到)

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

感谢您的帮助,-AJ

Thanks for any help, -AJ

更新1
好​​的,但我不应该声明第三行 public CircleR(double r) 作为双倍,对吗?在我正在学习的书中,示例没有这样做。

Update 1: Okay, but I shouldn't have to declare the third line public CircleR(double r) as a double, right? 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. 
     } 

正如你所看到的,代码 public Circle(double r).... 与我在<$ c中的做法有什么不同? $ c> public CircleR(double r)?无论出于何种原因,书中的代码都没有给出任何错误,但是我说这里有错误。

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)? For whatever reason, no error is given in the code from the book, however mine says there is an error there.

推荐答案


正如你所看到的,代码public Circle(double r)....
与我在公共CircleR(双r)中所做的不同?对于
无论什么原因,书中的代码都没有给出错误,但是
mine说那里有错误。

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

是正确的你的代码

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) 

public Circle(double r)

或(如果你真的想把你的构造函数命名为CircleR)重命名你的类到CircleR。

or (if you really wanted to name your constructor as CircleR) rename your class to CircleR.

所以你的新课应该是

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

我还在方法中添加了返回类型double Froyo和John B.指出。

I also added the return type double in your method as pointed out by Froyo and John B.

请参阅此文章

HTH。

这篇关于Java错误 - “无效的方法声明;需要返回类型“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆