Vala:如何引用抽象类'子类?

时间:2017-04-09 11:38:25

标签: singleton abstract vala

请考虑以下事项:

abstract class Singleton : Object {
    private static Singleton _instance = null;
    public static Singleton instance() {
        if (instance == null) {
            instance = // constructor call goes here
        }
        return instance;
    }
}

class Foo : Singleton {
    Foo() {}
}

var test = Foo.instance();

我想在抽象类中实现一个单例。我的问题是:如何从Singleton.instance()

引用子类构造函数

1 个答案:

答案 0 :(得分:0)

继承和单身模式不能很好地混合:

C++ Singleton class - inheritance good practice

为什么不让你的Foo类静态(让所有成员保持静态):

public class Foo {
    // Static constructor
    public static Foo() {}
    // Static methods
    public static void bar () {}
    // Static attributes, etc.
}

这通常也有效。如果你的"单身"它不会那么好用。有一个昂贵的结构,你想避免它总是被构造。