Java 如何为自定义侦听器编写事件类?

Java 如何为自定义侦听器编写事件类?,java,events,character,listeners,Java,Events,Character,Listeners,我正在用Java做一个游戏,我需要一些角色来监听另一个类中处理的时间事件 我一直用它来做我自己的听众 因此,我定义了这样一个接口: public interface TimeListener { public void onTick(TimeEvent event); } public class TimeEvent { private int time; public TimeEvent(int time) { this.time = time;

我正在用Java做一个游戏,我需要一些角色来监听另一个类中处理的时间事件

我一直用它来做我自己的听众

因此,我定义了这样一个接口:

public interface TimeListener {
    public void onTick(TimeEvent event);
}
public class TimeEvent {
    private int time;

    public TimeEvent(int time) {
        this.time = time;
    }

    public int getTime() {
        return time;
    }
}
时间事件是这样的:

public interface TimeListener {
    public void onTick(TimeEvent event);
}
public class TimeEvent {
    private int time;

    public TimeEvent(int time) {
        this.time = time;
    }

    public int getTime() {
        return time;
    }
}
在文章中,他写道EatEvent可以像食品包装器类一样简单。我不知道该怎么做。我的时间事件包括这个吗


与EateEvent不同,我的TimeEvent还需要将时间从time类传递给侦听器。这是发送时间的正确方法吗?

只需使用观察者设计模式即可