我是 Vala 的新手,到目前为止我认为它很酷,但我在理解继承方面遇到了困难。我读here我应该使用base()
调用父构造函数。好吧,很酷,似乎可以理解,但它对我不起作用。我一直收到标题上的错误。这是我要展示的片段:
public class MyBox : Gtk.Box {
public MyBox(Gtk.Orientation orientation, int spacing) {
// I have to this
this.set_orientation(orientation);
this.set_spacing(spacing);
// I want to do this:
base(orientation, spacing);
//workaround is this:
Object(orientation: orientation, spacing: spacing);
}
}
请帮助我理解为什么 Object(....) 有效但 base(...) 无效
不应该是同一件事吗?
这是由于 C 代码的实现造成的。当Vala生成构造函数时,它生成两个C函数a_new
分配内存并调用的函数_construct
and a _construct
初始化对象的函数。当您使用基本构造函数时base()
,它需要一个匹配_construct
要调用的函数。并非所有用 C 编写的类都有这个;在VAPI文件中,你会发现has_construct_function = false
对于一些构造函数。如果是这种情况,则无法进行连锁操作。基地GObject
可以从参数设置属性,因此这成为在基类中设置默认值的唯一方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)