import java.util.*;//import of java.util.event
//Declaration of the event's interface type, OR import of the interface,
//OR declared somewhere else in the package
interface ThrowListener {
public void Catch();
}
/*_____________________________________________________________*/class Thrower {
//list of catchers & corresponding function to add/remove them in the list
List<ThrowListener> listeners = new ArrayList<ThrowListener>();
public void addThrowListener(ThrowListener toAdd){ listeners.add(toAdd); }
//Set of functions that Throw Events.
public void Throw(){ for (ThrowListener hl : listeners) hl.Catch();
System.out.println("Something thrown");
}
////Optional: 2 things to send events to a class that is a member of the current class
. . . go to github link to see this code . . .
}
类文件中需要2件东西才能接收来自类的事件
/*_______________________________________________________________*/class Catcher
implements ThrowListener {//implement added to class
//Set of @Override functions that Catch Events
@Override public void Catch() {
System.out.println("I caught something!!");
}
////Optional: 2 things to receive events from a class that is a member of the current class
. . . go to github link to see this code . . .
}
# 1 楼答案
有三种不同的设置方法:
Thrower
在Catcher
Catcher
在Thrower
的内部Thrower
和Catcher
在本例中的另一个类内Test
THE WORKING GITHUB EXAMPLE I AM CITING默认为选项3,要尝试其他方法,只需取消对要成为main的类的“
Optional
”代码块的注释,并将该类设置为build.xml
文件中的${Main-Class}
变量:抛出代码需要4件事:
类文件中需要2件东西才能接收来自类的事件