基本訊號
訊號僅適用於 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");
}