有什么方法可以在 Vala 中执行以下操作吗?
public int year {
get { return this.year; }
set requires (1500 < value && value < 2050) { this.year = value; }
}
最佳答案
不,但“requires”实际上只是 GLib.return_if_fail 的语法糖。和 GLib.return_val_if_fail ,所以你可以这样做:
public int year {
get { return this.year; }
set {
GLib.return_if_fail (1500 < value && value < 2050);
this.year = value;
}
}
关于vala - 需要 vala 中的 setter/getter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13149535/