2017-10-05 27 views
0

私は特定の時間にステージに追加する俳優を持っています。そして、それがステージに追加された後、価値の計算が必要になります。 アクターがステージに追加された後に、そのアクターにコールバックを追加する方法はありますか?ステージに追加されたアクタにコールバックを追加するにはどうすればよいですか?

例コード

public class SlotReel extends Table{ 

    public SlotReel() { 
    } 

    public void compute(){ 
     //call after SlootReel is added to stage 
    } 

} 

例ステージ追加コード

stage.addActor(slotReel);// I wish to trigger the compute method in SlotReel after here. 

答えて

0

public class Solution { 

    // Create interface 
    interface Computable { 
     void compute(); 
    } 

    // SlotReel implement Computable interface 
    static public class SlotReel implements Computable { 

     String name; 

     public SlotReel(String name) { 
      this.name = name; 
     } 

     // Implement compute method 
     @Override 
     public void compute() { 
      // call after SlootReel is added to stage 
      // Just an example 
      System.out.println("Hello " + name); 
     } 

    } 

    static public class Stage { 

     List<Computable> list = new ArrayList<>(); 

     public void addActor(Computable slot) { 
      list.add(slot); 
      // call compute method 
      slot.compute(); 
     } 
    } 

    public static void main(String[] args) { 
     Stage stage = new Stage(); 
     stage.addActor(new SlotReel("A")); 
     stage.addActor(new SlotReel("B")); 
     stage.addActor(new SlotReel("C")); 
     stage.addActor(new SlotReel("D")); 
    } 
} 
関連する問題