Swift支持静态类吗? [英] Are static classes supported by Swift?

查看:155
本文介绍了Swift支持静态类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以像C#中那样快速创建静态类,即无法实例化的类,该类只能具有静态函数和静态属性.这些类型的课程可以迅速进行吗?

I would like to know if you can create static classes in swift similar to those in C# i.e. a class that cannot be instantiated which can only have static functions and static properties. Are these types of classes possible in swift?

如果没有,考虑到Swift中可用的工具,重新创建这种设计模式的最有效方法是什么?

If not, what is the most efficient way to recreate such a design pattern given the tools available within Swift?

推荐答案

不,Swift没有静态类的概念.但是,如果您仍然没有可分配的属性,初始化将产生什么不同?我可以想到2种选择:

No, Swift has no concept of a static class. But if you have no assignable property anyway, what difference will initialization make? I can think of 2 options:

  • 将类标记为final,这样它就不能被继承:final class MyClass { .... }

  • Mark the class as final so it cannot be inherited: final class MyClass { .... }

使用没有继承的struct:struct MyUtilities { ... }

这篇关于Swift支持静态类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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