public ApplicationEvent(Object source) { super(source); this.timestamp = System.currentTimeMillis(); }
public long getTimestamp() { return timestamp; }
} public abstract class ApplicationEvent extends EventObject { private final long timestamp; public ApplicationEvent(Object source) { super(source); this.timestamp = System.currentTimeMillis(); } public long getTimestamp() { return timestamp; } }如上所示,抽象类ApplicationEvent中timestamp字段存贮事件发生的时间,在该事件发生时(事件类 被实例化),ApplicationEvent会自动的把当前的时间存储到该字段中,在应用中可以通过 getTimestamp()方法获取该时间。如果应用要传递一个对象给监听器,那么可以把要传递的对象作为构造 函数的参数source传递给ApplicationEvent类。