Vala中基于类的枚举?

问题描述 投票:0回答:1

我想知道如何在Vala中创建基于类的枚举。

在Java中,您可以执行以下操作:

public class Main {
    public static void main(String[] args) {
        Action action = Action.COMPRESS;
        System.out.printf("Action name: %s, index %d", action.getName(), action.getIndex());
    }
}

class Action {

    public static final Action COMPRESS = new Action("Compress", 60);
    public static final Action DECOMPRESS = new Action("Decompress", 70);

    private String name;
    private int index;

    private Action(String name, int index) {
        this.name = name;
        this.index = index;
    }

    public String getName() {
        return name;
    }

    public int getIndex() {
        return index;
    }
}

但是当我在Vala中尝试以下操作时,COMPRESSDECOMPRESS始终从访问Action类之外时访问]为空。

public static int main(string[] args) {
    stderr.printf("Action name: %s\n", UC.Action.COMPRESS.get_name());
}

public class UC.Action : GLib.Object {

    public static UC.Action COMPRESS   = new UC.Action("Compress");
    public static UC.Action DECOMPRESS = new UC.Action("Decompress");

    private string name;

    [CCode (construct_function = null)]
    private Action(string name) {
        this.name = name;
    }

    public string get_name() {
        return name;
    }
}

该代码输出以下内容:Performing (null)

任何想法如何做到这一点?

我想知道如何在Vala中创建基于类的枚举。在Java中,您可以执行以下操作:public class Main {public static void main(String [] args){Action action = Action.COMPRESS; ...

vala
1个回答
0
投票

在Vala中,静态类成员是在class_init GObject函数期间初始化的,因此在调用它们之前不可用。

© www.soinside.com 2019 - 2024. All rights reserved.