super()
생성자에 아무것도 전달하지 않고 a와 같은 작업을하고 싶습니다 .
클래스 내부의 메서드로 설정된 현재 상태의 모든 변수를 복사하는 것과 같은 것입니다. 래퍼 클래스가 추가 메서드를 사용하는 Player 일 때 래퍼 클래스
와 같은 작업을 피하고 싶습니다 getPlayer()
.
Player 클래스의 일부가 아니기 때문에 이러한 추가 메서드를 Player 클래스로 옮기고 싶지 않습니다. Player 클래스가 들어갈 수 있지만 항상 그런 것은 아닙니다.
나는 여전히 내가 전달하는 인스턴스에서 Player로 유지하면서 설정해야하는 자체 변수로 SpecialPlayer의 생성자를 실행할 수 있기를 원합니다. 그것이로 주조하는 동안에 클래스 변수는 언제든지 변경 될 수 있습니다Player
SpecialPlayer
클래스와 SpecialPlayer
클래스는 Player
항상 같은 최신 변수를 가져야합니다 . 예.보기에 의해 항상 정확히 동일한 참조 를 사용해야 합니다.
다음은 문제를 설명하기 위해 작성한 의사 코드입니다.
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.
나는 이것이 당신이 원하는 일이라고 생각합니다.
public SpecialPlayer(Player player) {
super(player.money);
bar=player.bar;
foo = System.currentTimeMillis();
}
새로운 사람 specialPlayer
은 플레이어의 돈과 막대를 전달받습니다.
또는 모든 필드를 Player
전달 된 것과 동일하게 설정하려면 다음을 시도 할 수 있습니다.
public SpecialPlayer(Player player) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
super(player.money); //Still need to do this
for(Field sfield:player.getClass().getDeclaredFields();){
sfield.setAccessible(true);
sfield.set(this, sfield.get(player));
}
}
이렇게하면 전달 된 필드의 값으로 SpecialPlayer
선언 된 모든 새 필드가 설정됩니다 .Player
player
원본의 상태를 변경하고 업데이트가 필요할 Player
때 새 .NET에 반영 SpecialPlayer
되도록하려면 원본 Player
에 대한 모든 참조를 변경 하여 새 SpecialPlayer
.
Player player= new Player(123);
SpecialPlayer special = new SpecialPlayer(player);
player=special;
또 다른 생각은, 정말로 SpecialPlayer
오래된 것과 같은 모든 분야로 새로운 것을 만들고 싶습니까 Player
, 아니면 실제로 오래된 것을 Player
특별 하게 만들고 싶 습니까? SpecialPlayer
생성자를 다음과 같이 만드는 것이 더 쉬울 수 있습니다 .
public SpecialPlayer(int money){
super(money);
}
그런 다음 나중에 특별해질 수있는 모든 플레이어를 위해 다음과 같이 만들 수 있습니다.
Player player1 = new SpecialPlayer(123);
Player player2 = new SpecialPlayer(345);
그런 다음 SpecialPlayer
특별하기를 원할 때 캐스팅하십시오 .
SpecialPlayer special = (SpecialPlayer)player1;
이렇게하면 플레이어를 특별하게 만들 때 참조를 변경하거나 새 개체를 만들 필요가 없습니다.
SpecialPlayer
생성자가 현재 수행하는 작업을 수행 하는 메서드를 추가하고 해당 플레이어를 특별하게 만들고 싶을 때마다 호출 할 수도 있습니다.
public void setSpecial(){
foo = System.currentTimeMillis();
}
.
SpecialPlayer special = (SpecialPlayer)player1;
special.setSpecial();
플레이어가 setSpecial()
재정의 된 메서드를 사용해야하는지 슈퍼 메서드로 되돌려 야하는지 구별해야 할 때 토글 할 수있는 플래그가 필요할 수도 있습니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다