基本信号
信号仅适用于 GObject 类。它们只能是公共的,这意味着代码的任何部分都可以连接处理程序并触发它们。
public class Emitter : Object {
// A signal is declared like a method,
// but with the signal keyword.
public signal void my_signal ();
public void send_signal () {
this.my_signal (); // Send a signal by calling it like a method.
}
}
void main () {
var emitter = new Emitter ();
// Use the connect method of the signal to add an handler.
emitter.my_signal.connect (() => {
print ("Received the signal.\n");
});
emitter.send_signal ();
emitter.my_signal (); // You can send a signal from anywhere.
}
如果它们与信号具有相同的签名,你还可以使用普通函数作为处理程序。
void main () {
var emitter = new Emitter ();
emitter.connect (my_handler);
emitter.my_signal ();
}
void my_handler () {
print ("Received the signal.\n");
}