简体   繁体   English

Java基本编译错误

[英]Java basic compilation error

I have a simple error in a very simple start of a program that has me stumped. 在一个我很困惑的程序的非常简单的启动中,我有一个简单的错误。 I've included comments of the errors on the appropriate lines. 我在相应的行中包含了有关错误的注释。 What am I missing? 我想念什么? (Yes, I am a nube.) (是的,我是个小家伙。)

package mainPack;

public class Bodies{

    int mass;
    int radius;
    int xpos;
    int ypos;
    float xvel;
    float yvel;   //Syntax error, insert "}" to complete ClassBody

    public Bodies(mass, radius, xpos, ypos, xvel, yvel){
    }

}   //Syntax error on token "}", delete this token

Your issue is that the parameters in your constructor have no data types. 您的问题是构造函数中的参数没有数据类型。

NB: 注意:
Since your parameter names are the same as your instance variable names, you will need to use this , as in: 由于参数名称与实例变量名称相同,因此需要使用this ,如下所示:

   public Bodies(int mass, int radius, int xpos, int ypos, float xvel, float yvel)
    {
        this.mass = mass;
        this.radius = radius;
        //...
    }

where this.mass refers to the instance variable mass , as opposed to the passed parameter of the constructor. 其中this.mass指向实例变量mass ,与构造函数的传递参数相反。

For more information, check out the Oracle Tutorial on Java Constructors . 有关更多信息,请查看有关Java构造函数Oracle教程

As an aside, on float also from Oracle : 顺便说一句,Oracle也提供float

As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. 与字节和短的建议一样,如果需要在大型浮点数数组中保存内存,请使用浮点数(而不是双精度数)。 This data type should never be used for precise values, such as currency. 永远不要将这种数据类型用于精确值,例如货币。

When declaring the constructor, you need to specify the types of its arguments: 在声明构造函数时,需要指定其参数的类型:

public Bodies(int mass, int radius, int xpos, int ypos, float xvel, float yvel) {

Having done this, you also need to initialize the data members: 完成此操作后,您还需要初始化数据成员:

public Bodies(int mass, int radius, int xpos, int ypos, float xvel, float yvel) {
   this.mass = mass;
   ...

您必须在构造函数中定义参数的类型及其名称。

package mainPack;

public class Bodies{

    int mass;
    int radius;
    int xpos;
    int ypos;
    float xvel;
    float yvel;

    public Bodies(int mass, int radius, int xpos, int ypos, float xvel, float yvel){
        this.mass = mass;
        this.radius = radius;
        this.xpos = xpos;
        this.ypos = ypos;
        this.xvel = xvel;
        this.yvel = yvel;
    }

}

You were missing the types for the arguments for the constructor. 您缺少构造函数的参数类型。 You will probably want to initialize the fields in the constructor, so I did it also. 您可能要初始化构造函数中的字段,所以我也这样做了。

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

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