Abstract class
The abstract class gets the construction parameters of the subclass
This idea comes from: ApplicationEvent
When customizing Spring events, define it like this:
package com.example.tmp;
import org.springframework.context.ApplicationEvent;
public class MyEvent extends ApplicationEvent {
public MyEvent(Object source) {
super(source);
}
}
If you don't add this parameterized constructor, an error will be reported:
Source code
public abstract class ApplicationEvent extends EventObject { private static final long serialVersionUID = 7099057708183571937L; private final long timestamp; public ApplicationEvent(Object source) { super(source); this.timestamp = System.currentTimeMillis(); } public final long getTimestamp() { return this.timestamp; } }
public class EventObject implements java.io.Serializable { private static final long serialVersionUID = 5516075349620653480L; protected transient Object source; public EventObject(Object source) { if (source == null) throw new IllegalArgumentException("null source"); this.source = source; } public Object getSource() { return source; } public String toString() { return getClass().getName() + "[source=" + source + "]"; } }
ApplicationEvent class relationship
to sum up
Spring finally uses EventObject to store event data (source field), which has an abstract subclass (ApplicationEvent in this case), and lets the abstract subclass super parent in the constructor, and the parameter is the event data.
On the one hand: the ordinary class that inherits ApplicationEvent must provide a parameterized constructor, and super() must be used to call the constructor of the Abstract class ApplicationEvent (because the abstract class itself cannot be instantiated directly, it must be instantiated by the subclass)
On the other hand: Because the ApplicationEvent abstract class is super parent class in the constructor, and the parameter is event data, EventObject can be stored in event data.
EventObject is a base class, which has many subclasses (all abstract classes), and ApplicationEvent is one of the subclasses. EventObject saves event data, and the subclasses will have some extensions, and the subclasses are like ApplicationEvent, with parameter constructors, and super calls the EventObject constructor in it.
interface
Multiple implementation classes implement the same interface
interface ITop
interface IA extends ITop
class AImpl implements IA
interface IB extends ITop
class BImpl extends AImpl implements IB