簡體   English   中英

如何將回調添加到actor時添加到舞台上?

[英]How to add a callback to actor when it is added to stage?

我有一個在特定時間加入舞台的演員。 將值添加到階段后,需要對值進行計算。 在將Actor添加到舞台之后,是否可以將回調添加到Actor

范例程式碼

public class SlotReel extends Table{

    public SlotReel() {
    }

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

}

Example Stage添加代碼

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

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"));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM