让这个类从不直接使用,只继承:
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
}