I want to do something like a super()
but without passing anything to the constructor.
Something like copying all the variables in their current states, which were set to methods inside that class.
I want to avoid doing stuff like getPlayer()
in a wrapper class when the wrapper class is the Player just with extra methods.
I don't want to move these extra methods into the Player class because it's not part of the Player class it's something a Player class may become into but not at all times.
I still want to be able to run constructor of the SpecialPlayer with it's own variables it has to set while still maintaining it as a Player from whatever instance I pass into it.
The Player
class variables may change at any time while it's casted into a SpecialPlayer
class and the SpecialPlayer
class should have up to date variables same as Player
at all times.. Yes it has to the same reference exactly at all times by the looks of it.
Here is some pseudo code I wrote to try to illustrate the problem
public class Player {
private int money;
private boolean bar;
public Player(int money) {
this.money = money;
bar = true;
}
public void toggleBar() {
bar != bar;
}
}
public class SpecialPlayer extends Player {
private Player player;
private long foo;
public SpecialPlayer(Player player) {
this.player = player; //wrapper way not good...
//this = player; ??? doesn't compile...
foo = System.currentTimeMillis();
}
public long getFoo() {
return foo;
}
public Player getPlayer() { //<-- this is stupid.
return player;
}
}
Player player = new Player(12345);
player.toggleBar(); //it's now false, also it's not a constructor variable.
SpecialPlayer specialPlayer = new SpecialPlayer(player);
specialPlayer.getFoo(); //some long number value.
specialPlayer.toggleBar(); //now it should be true!.
player.toggleBar(); //should be false.
specialPlayer.toggleBar(); //should be true.