This site requires JavaScript, please enable it in your browser!
Greenfoot back
decadence18
decadence18 wrote ...

2015/11/17

Using a Non-default Constructor with an Actor?

decadence18 decadence18

2015/11/17

#
Is this possible? I ask because I set the image of a piece using a String, and I need a way to pass the String from one class to another. Essentially, what I'm doing is loading a file with the name of a piece, the x location of the piece, and the y of the piece. The piece is just "Piece.class". I need to be able to pass the name, x, and y of the piece to it from the LoadGame class. Is there a way to use a non-default constructor to where I can pass it the name, x, and y; or will I need to create a "setter" to change the name, x, and y after creation? If you need to see my code, I can post it. I didn't see a purpose in adding it since I'm not sure if you(all) would need it, and it would clutter up the question if you didn't. Thank you.
danpost danpost

2015/11/17

#
Of course you can do that. The World class requires three int values (or three int values and a boolean) for when you create a World object. You can have any class you create require specific data for creation.
decadence18 decadence18

2015/11/17

#
Hmm. I was able to do it with a World, but when I attempted to do it with an Actor it bypassed by non-default constructor. Do actors require a default constructor even if there is a non-default constructor? Because I didn't have a default when I attempted it the last time.
danpost danpost

2015/11/17

#
You really should show the code you are having issues with anytime you refer to any.
danpost danpost

2015/11/17

#
Or, you can just refer to the java tutorials for the information you are asking about. See this on constructors.
decadence18 decadence18

2015/11/17

#
I'm currently enrolled in a class where we learn Java. I'm asking about the properties of the program, not the properties of the language itself. I was unable to find documentation where it provides if there are instances where non-default constructors are ignored, and as the program was ignoring one of my constructors I thought I should ask. I also stated that if anyone needed to see my code to ask, because I didn't want to clutter up the question with code when the question, again, concerns the program more than the code itself.
danpost danpost

2015/11/17

#
The link I gave in the other discussion thread has all the information necessary for this question. For specific help, you do need to post the entire code of the class of the object you are trying to create and the code you are using to create an object of that class.
danpost danpost

2015/11/17

#
To help: anytime you add a non-default constructor to a class, you forfeit direct access to the default constructor of its superclass. You could add the following to the class to regain access to it ( class name is 'ClassName' ):
1
2
3
4
public ClassName()
{
    super();
}
If you needed to have the default constructor run and then do other things specific to the subclass you can insert that code after line 3 above.
You need to login to post a reply.