swift中结构体与枚举_在Swift 5中取消结构,类和枚举的神秘化

swift中结构体与枚举

介绍 (Introduction)

Every newbie has once suffered from the hardships of making a difference between those three attributes. However, struct, class, and enum are fundamentally important concepts for every iOS developer. It’s difficult to imagine common iOS apps without them. Let’s clear all doubts and explore every aspect of them so that in the end you will have a deep understanding of these concepts.

每个新手都曾经历过在这三个属性之间做出区别的艰辛。 但是,结构,类和枚举对于每个iOS开发人员都是根本重要的概念。 很难想象没有它们的常见iOS应用程序。 让我们清除所有疑问并探索它们的各个方面,以便最终您将对这些概念有深刻的理解。

结构体 (Structures)

We need structures to hold and manage a certain amount of data, which depends on a similar topic, in one place. It is possible to comprise in struct data types as well as methods. Let’s take a look at the following example:

我们需要一个结构来在一处保存和管理一定数量的数据,这取决于类似的主题。 可以包含结构数据类型和方法。 让我们看下面的例子:

变异方法 (Mutating methods)

Structs have also got so-called mutating methods that play a role in updating the property values of a structure within an instance method.

结构还具有所谓的变异方法 ,它们在实例方法中更新结构的属性值时起作用。

班级 (Classes)

Classes and structures are very similar, and both can be used to define properties and methods. Now, let’s consider the main differences between classes and structs.

类和结构非常相似,并且都可以用来定义属性和方法。 现在,让我们考虑一下类和结构之间的主要区别。

继承和替代方法和属性 (Inheritance & Override methods and properties)

The biggest difference that structs do not have is hierarchical relations. Classes can have parent classes, that are called superclass, and child classes, that are called subclasses. As it is in family relationships, subclasses can inherit properties and methods from classes and superclasses

结构没有的最大区别是层次关系。 类可以具有父类(称为超类 )和子类(称为子类) 。 就像在家庭关系中一样,子类可以从类和超类继承属性和方法

The other difference is that in classes, there is a possibility to augment or rewrite the data in the base class, which were written in the superclass.

另一个区别是,在类中,有可能在基类中扩充或重写在超类中编写的数据。

In the example above, class Animal is a grandparent — superclass, where class LandAnimals is a parent — base class and class Cat is a child — subclass.

在上面的示例中,Animal类是祖父母( 类) ,其中LandAnimals类是父类(基类) ,Cat类是子类(子类)。

总结思想 (Closing Thoughts)

Now, I hope you can clearly see differences and common grounds in structs, classes and enums. If there are still some questions that bother you, feel free to leave a comment.

现在,我希望您能清楚地看到结构,类枚举之间的差异和共同点 如果仍有其他问题困扰您,请随时发表评论。

Thanks for reading!

谢谢阅读!

翻译自: https://levelup.gitconnected.com/demystifying-struct-class-and-enum-in-swift-5-43dde089e96a

swift中结构体与枚举

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值