添加构造函数会出错?这没有意义,请帮忙,编程问题
我收到语法错误“插入}来完成 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()
{
}
}
但是,当我添加这样的构造函数时,我收到错误:
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;
}
}
**注意,我正在处理中编写此代码!
即使我移动枚举行到顶部(“Type type;”上方),然后错误消息跳转到“Rectangle location;”行
,所以我不明白为什么我没有收到第一个代码的错误?但我做第二个!
更新
好的,我更改了代码以使枚举初始化每个构造函数中的类型变量,这将用于我正在帮助设计的类项目。还有另一个名为 GameEvent 的类,其中有一个触发器实例和一个动作数组列表。触发器将从文件上传,然后对动作进行硬编码(我知道风格很糟糕,但只有 3 个任务,而且助教说我们不会因此而丢分)所以儿童课程听起来是个好主意,但为什么它不起作用呢?
这是更新后的代码:
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;
}
}
I am getting a syntax error "insert } to complete ClassBody.
This code works ok/ error free:
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()
{
}
}
However when I add in constructors like this I get the error:
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;
}
}
**Note that I am writing this code in processing!
also if I move the enum line to the top (above "Type type;") then the error message jumps to the line "Rectangle location;"
so what is going on here? I don't understand why I do not get an error for the first code but i do for the second!
Update
ok I changed the code to make the enum initialize the type variable in each constructor. This is going to be for an rts I am helping to design for a class project. There is another class called GameEvent that has have an instance of trigger in it and an array list of actions. The triggers will be uploaded from a file and then the actions will be hardcoded ( I know bad style but there are only 3 missions and the TA said that we wouldn't be losing marks for doing that). So children classes sounds like a good idea. But how come it isn't working as is?
here's the updated code:
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的构造函数不正确。例如:
您希望 Type.LOCATION 部分在参数列表中做什么?每个参数都应该是一个类型,后跟参数名称(如
int locx
等,正确的是)。您是否尝试根据调用者是否尝试指定位置、时间等来添加不同的构造函数?如果是这样,那绝对不是你这样做的......但听起来你可能想要为每种情况单独的类。
Your constructors are incorrect. For example:
What do you expect the Type.LOCATION part to do in the parameter list? Each parameter is meant to be a type followed by the name of the parameter (as
int locx
etc are, correctly).Were you trying to add different constructors depending on whether the caller was trying to specify a location, a time etc? If so, that's definitely not how you do it... but it sounds like you probably want separate classes for each of those cases anyway.
我建议不要以这种方式编写构造函数。
根据您调用的构造函数,您的对象最终会处于不同程度的不可用状态。一个物体在建造完成后应该 100% 准备好投入使用,而不需要做任何让客户感到惊讶的事情。
编写一个初始化所有成员变量的构造函数,然后使用合理的默认值从其他构造函数中调用“this”。
I would advise against writing your constructors this way.
Your object ends up in different degrees of unusable state depending on which constructor you call. An object ought to be 100% ready to go after construction, without doing anything to surprise clients.
Write one constructor that initializes ALL the member variables, and then call "this" from the others with sensible default values.
你不能像这样使用 Enum。当你创建一个枚举时,你创建了一个类型,所以你的构造函数必须采用一个变量,它是你的枚举的类型,你不能像这样“强制”它。
我不明白你想要做什么,但你应该阅读一些有关 Enums 的文档: http://download.oracle.com/javase/1.5.0/docs/guide/language/enums.html
在我看来,你最好做一些继承你的触发器类。将许多不同的字段放在一个类中并根据某些枚举类型初始化它们并不是真正的面向对象。
您可以轻松地拥有一个父触发器类,然后为您需要的每个触发器类型创建一个子类。
You can't use Enum like this. When you create an Enum, you create a Type, so your constructor must take a variable which is the type of your Enum, you can't "force" it like this.
I don't grasp what you're trying to do, but you should read some documentation about Enums : http://download.oracle.com/javase/1.5.0/docs/guide/language/enums.html
And it seems to me that you better do some inheritance of your Trigger class. It's not really OO to put a lot of different fields in a class and initialize them depending on some Enum type.
You can easily have a parent Trigger class and then create a children for each Trigger type you need.
你的代码没有意义。方法(包括构造函数)的参数列表必须是类型变量列表,而不是值。
Type.TIMED
是一个值,而不是类型!Your code doesn't make sense. The argument list for a method (including constructors) must be a list of typed variables, not values.
Type.TIMED
is a value, not a type!k 我将构造函数更改为这样,它可以工作:
Trigger(Type tT, long t, int c, int locx, int locy, int w, int h, int resT, int resN)
{
}
k I changed my constructors to this and it works:
Trigger(Type tT, long t, int c, int locx, int locy, int w, int h, int resT, int resN)
{
}