不支持链接到"Gtk.Box.new" [英] Chain up to 'Gtk.Box.new' not supported

查看:80
本文介绍了不支持链接到"Gtk.Box.new"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Vala的新手,到目前为止,我认为它很酷,但是我在理解继承方面遇到困难.我在此处中读到,我应该使用base()来调用父级构造函数.好了,很酷,似乎可以理解,但是对我来说不起作用.我一直在标题上得到错误.这是我要显示的摘录:

I'm new to Vala and so far I think it's pretty cool but I'm having trouble understanding inheritance. I read here that I should use base() to call the parents constructor. Alright, cool, seems understandable but It din't work for me. I kept getting the error on the title. Here is my snippet to show:

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(...)

Please help me understand why Object(....) works but not base(...)

不是同一回事吗?

推荐答案

这是由于C代码的实现所致.当Vala生成构造函数时,它将生成两个C函数,一个_new函数分配内存并调用_construct,另一个_construct函数初始化该对象.当您使用base()设置基本构造函数时,它需要一个匹配的_construct函数来调用.并非所有用C编写的类都具有此功能.在VAPI文件中,对于某些构造函数,您会找到has_construct_function = false.如果是这种情况,则无法进行连锁.基本的GObject可以从参数设置属性,因此这成为在基类中设置默认值的唯一方法.

This is due to implementation of the C code. When Vala generates a constructor, it generates two C functions a _new function that allocates memory and calls the _construct and a _construct function that initialises the object. When you case the base constructor using base(), it needs a matching _construct function to call. Not all the classes written in C have this; in the VAPI file, you will find has_construct_function = false for some constructors. If this is the case, no chain-up can be done. The base GObject can set properties from arguments, so this becomes the only way to set defaults in the base class.

这篇关于不支持链接到"Gtk.Box.new"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆