您可能希望通过3种不同的方式进行设置:
Thrower
代替 Catcher
Catcher
代替 Thrower
Thrower
和Catcher
内部在这个例子中另一个类的Test
我正在使用的GITHUB示例示例默认值为选项3,要尝试其他选项,只需取消注释Optional
要成为主类的“”代码块的注释,然后将该类设置为文件中的${Main-Class}
变量build.xml
:
抛出边码所需的4件事:
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 . . .
}