adding constructors makes an error? This doesn't make sense please help, programming question
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;
}
}
Your constructors are incorrect. For example:
Trigger(Type.LOCATION, int locx, int locy, int w, int h)
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.
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.
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.
Trigget(Type t, [others arguments]) {
}
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.
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 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)
{
type = tT;
if (type == Type.TIMED)
time = t;
else if (type == Type.CONTROLLED)
controlNum = c;
else if (type == Type.LOCATION)
location = new Rectangle(locx, locy, w, h);
else if (type == Type.RESOURCE)
{
resType = resT;
resNum = resN;
}
}
精彩评论