让这个类从不直接使用,只继承:
class ApiBase {}
我如何default在这里定义一个通用的静态单例?- 所以我可以:
class FooApi: ApiBase {} // Want only one instance of `FooApi`
class BarApi: ApiBase {} // Want only one instance of `BarApi`
FooApi.default.foo_api_only_method_name()
BarApi.default.bar_api_only_method_name()
我唯一能想到的就是创建一个protocol需要实现FooApi的内容。BarApi但这似乎不是最理想的,宁愿写一个像这样的实现:
func `default`<T: ApiBase>() -> T {
if staticInstance == nil {
staticInstance = T()
}
return staticInstance
}