package listener;
interface LoveListener {
/**
*
* description: 监听爱上别人事件
*/
public void loves(LoveEvent e);
/**
*
* description: 监听被爱事件
*/
public void beLove(LoveEvent e);
}
class Person {
private LoveListener loveListener;
private LoveEvent loveEvent = new LoveEvent();
public void addLoveListener(LoveListener loveListener) {
this.loveListener = loveListener;
//设置事件源
loveEvent.setObj(this);
}
public void love() {
if (null != loveListener) {
loveListener.loves(loveEvent);
}
}
public void beLove()
{
if(null!=loveListener){
loveListener.beLove(loveEvent);
}
}
}
class LoveEvent {
private Object obj = null;
public Object getSource() {
return obj.toString();
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
}
public class someOne extends Person implements LoveListener {
public someOne() {
this.addLoveListener(this);
//触发事件
this.love();
}
public void testClick()
{
this.addLoveListener(this);
this.beLove();
}
public void beLove(LoveEvent e) {
// TODO Auto-generated method stub
System.out.println("被人攻击了");
}
public void loves(LoveEvent e) {
// TODO Auto-generated method stub
System.out.println("你爱上了某人");
System.out.println("你爱上的人是:" + e.getSource());
}
public static void main(String[] args) {
someOne one=new someOne();
one=new someOne();
while(true)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
one.testClick();
}
}
}