0

次の基底クラス Queen と Knight がその派生クラスであるとします。WeaponBehaviour はインターフェースです。具体的な GameCharacter の種類によっては、Guice を使用して武器を注入する方法がわかりません。

public abstract class GameCharacter {
    @Inject
    protected WeaponBehaviour weapon;

    public GameCharacter() {

    }

    public void fight() {
        weapon.useWeapon();
    }

    public void setWeapon(WeaponBehaviour weapon) {
        this.weapon = weapon;
    }
}
4

1 に答える 1

6

バインディングアノテーションを使用できます。

サブクラス:

class GimliSonOfGloin extends GameCharacter {

    @Inject
    public void setWeapon(@Axe WeaponBehaviour weapon) {
        super.setWeapon(weapon);
    }
}

注釈:

@BindingAnnotation 
@Target({ FIELD, PARAMETER, METHOD }) 
@Retention(RUNTIME)
public @interface Axe {}

バインディング:

bind(WeaponBehaviour.class)
    .annotatedWith(Axe.class)
    .to(MyAxe.class);
于 2012-07-26T10:46:19.590 に答える