我收到语法错误"insert}以完成ClassBody.
此代码正常/无错误:
import java.awt.Rectangle;
class Trigger
{
Type type;
long time;
ObjectID controlType;
int controlNum;
int resType, resNum;
Rectangle location;
enum Type {TIMED, CONTROLED, LOCATION, RESOURCE};
Trigger()
{
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我添加像这样的构造函数时,我收到错误:
class Trigger
{
Type type;
long time;
ObjectID controlType;
int controlNum;
int resType, resNum;
Rectangle location;
enum Type {TIMED, CONTROLED, LOCATION, RESOURCE}; //I get the error on this line
Trigger(Type.TIMED, long t)
{
time = t;
}
Trigger(Type.CONTROLLED, int c)
{
controlNum= c;
}
Trigger(Type.LOCATION, int locx, int locy, int w, int h)
{
location = new Rectangle(locx, locy, w, h);
}
Trigger(Type.RESOURCE, int resT, int resN)
{
resType = resT;
resNum = resN;
}
}
Run Code Online (Sandbox Code Playgroud)
**请注意,我正在处理这段代码!
如果我将枚举行移到顶部("Type type;"上面),那么错误消息会跳转到"Rectangle location"行.
那么这里发生了什么?我不明白为什么我没有得到第一个代码的错误,但我做的第二个!
更新
好吧,我更改了代码,使枚举初始化每个构造函数中的类型变量.这将是我正在帮助设计课堂项目的rts.还有另一个名为GameEvent的类,它有一个触发器实例和一个动作数组列表.触发器将从文件上传,然后操作将被硬编码(我知道不好的风格,但只有3个任务,TA表示我们不会因此而失去标记).所以儿童课听起来是个好主意.但是为什么它不能正常工作呢?
这是更新的代码:
import java.awt.Rectangle;
class Trigger
{
Type type;
long time;
FCObjectID controlType;
int controlNum;
int resType, resNum;
Rectangle location;
enum Type {TIMED, CONTROLED, LOCATION, RESOURCE};
Trigger(Type.TIMED, long t)
{
type = TIMED;
time = t;
}
Trigger(Type.CONTROLLED, int c)
{
type = CONTROLED;
controlNum= c;
}
Trigger(Type.LOCATION, int locx, int locy, int w, int h)
{
type = LOCATION;
location = new Rectangle(locx, locy, w, h);
}
Trigger(Type.RESOURCE, int resT, int resN)
{
type = RESOURCE;
resType = resT;
resNum = resN;
}
}
Run Code Online (Sandbox Code Playgroud)
你的构造函数是不正确的.例如:
Trigger(Type.LOCATION, int locx, int locy, int w, int h)
Run Code Online (Sandbox Code Playgroud)
你期望Type.LOCATION部分在参数列表中做什么?每个参数都是一个类型,后跟参数的名称(int locx正确地说是等等).
您是否尝试添加不同的构造函数,具体取决于调用者是否尝试指定位置,时间等?如果是这样,那肯定不是你怎么做的......但听起来你可能想要为这些案例中的每个案例分别进行单独的课程.