Reputation: 389
I have 3 class level1
, level2
, and player
. In player
I have script like this
private level1 play;
(level1
is the name of class, and play
is variable). I want to ask, how to change/cast level1
to level2
in player
class without change the variable name.
Can I do that?
Upvotes: 2
Views: 5112
Reputation: 14617
What you need to do is to create an interface. For example, the following will help:
public interface Level { }
public class Level1 implements Level { }
public class Level2 implements Level { }
This way, you can do:
private Level play;
public void start() {
play = new Level1();
if(play.isFinished()) {
play = new Level2();
}
}
It's just some example code, but I hope you catch my drift.
Upvotes: 5