Object–Oriented Programming with Swift 2
上QQ阅读APP看书,第一时间看更新

Understanding the elements that compose a class

So far, we worked with a very simple class and many instances of this class in the Playground. Now, it is time to dive deep into the different members of a class.

The following list enumerates the most common element types that you can include in a class definition in Swift and their equivalents in other programming languages. We already worked with a few of these elements:

  • Initializers: These are equivalent to constructors in other programming languages
  • Deinitializer: This is equivalent to destructors in other programming languages
  • Type properties: These are equivalent to class fields or class attributes in other programming languages
  • Type methods: These are equivalent to class methods in other programming languages
  • Subscripts: These are also known as shortcuts
  • Instance properties: This is equivalent to instance fields or instance attributes in other programming languages
  • Instance methods: This is equivalent to instance functions in other programming languages
  • Nested types: These are types that only exist within the class in which we define them

We already learned how basic initializers and deinitializers work in the previous chapter. So far, we used an instance-stored property to encapsulate data in our instances. We could access the instance property without any kind of restrictions as a variable within an instance.

However, as it happens sometimes in real-world situations, restrictions are necessary to avoid serious problems. Sometimes, we want to restrict access or transform specific instance properties into read-only attributes. We can combine the restrictions with computed properties that can define getters and/or setters.

Tip

Computed properties can define get and/or set methods, also known as getters and setters. Setters allow us to control how values are set; that is, these methods are used to change the values of related properties. Getters allow us to control the values that we return when computed properties are accessed. Getters don't change the values of related properties.

Sometimes, all the members of a class share the same attribute, and we don't need to have a specific value for each instance. For example, the superhero types have some profile values, such as the average strength, average running speed, attack power, and defense power. We can define the following type properties to store the values that are shared by all the instances: averageStrength, averageRunningSpeed, attackPower, and defensePower. All the instances have access to the same type properties and their values. However, it is also possible to apply restrictions to their access.

It is also possible to define methods that don't require an instance of a specific class to be called; therefore, you can invoke them by specifying both the class and method names. These methods are known as type methods, operate on a class as a whole, and have access to type properties, but they don't have access to any instance members, such as instance properties or methods, because there is no instance at all. Type methods are useful when you want to include methods related to a class and don't want to generate an instance to call them. Type methods are also known as static or class methods. However, we have to pay attention to the keyword we use to declare type methods in Swift, because a type method declared with the static keyword has a different behavior than a type method declared with the class keyword. We will understand their difference as we move forward with the examples in this and the forthcoming chapters.