Creating values shared by all the instances of a class with type properties

The LionSuperHero class is a blueprint for lions that are superheroes. This class should inherit from the SuperHero class, but we will forget about inheritance and other super types of superheroes for a while and use the LionSuperHero class to understand the difference between type and instance properties.

We will define the following type properties to store the values that are shared by all the members of the lion superhero group:

  • averageStrength: This is the average strength of the superhero group.
  • averageRunningSpeed: This is the average running speed of the superhero group.
  • attackPower: This is the attack power score of the superhero group.
  • defensePower: This is the defense power score of the superhero group.
  • warriorScore: This is the score that combines the previously mentioned values in a single value that determines the warrior score of the superhero group. It is a calculated type property.

The following lines create a LionSuperHero class, declare the previously enumerated type properties, and declare two additional instance public properties named name and runningSpeedScore. The code file for the sample is included in the swift_3_oop_chapter_03_13 folder:

    public class LionSuperHero { 
 
      public static var averageStrength: Int = 10 
      public static var averageRunningSpeed: Int = 9 
      public static var attackPower: Int = 10 
      public static var defensePower: Int = 6 
      public static var warriorScore: Int { 
        return (averageStrength * 3) + (attackPower * 3) + 
        (averageRunningSpeed * 2) + (defensePower * 2) 
      } 
 
      public let name: String 
 
      private var runningSpeedScoreField: Int = 0 
      public var runningSpeedScore: Int { 
        get { 
          return runningSpeedScoreField 
        } 
        set { 
          if (newValue < 0) { 
            runningSpeedScoreField = 0 
          } else if (newValue > 50) { 
            runningSpeedScoreField = 50 
          } else { 
            runningSpeedScoreField = newValue 
          } 
        } 
      } 
     
      init(name: String, runningSpeedScore: Int) { 
        self.name = name 
        self.runningSpeedScore = runningSpeedScore 
      } 
    } 

The code initializes each type property in the same line that declares the field. The only difference between a type and instance property is the inclusion of the static keyword to indicate that we want to create a type property.

The following line prints the value of the previously declared averageStrength type property. Note that we didn't create any instance of the LionSuperHero class and that we specified the type property name after the class name and a dot. The code file for the sample is included in the swift_3_oop_chapter_03_13 folder:

    print(LionSuperHero.averageStrength) 

Tip

Swift doesn't allow us to access a type property from an instance; therefore, we always have to use a class name to access a type property.

You can assign a new value to any type property declared with the static and var keywords. For example, the following lines assign 9 to the averageStrength type property and print the new value. The code file for the sample is included in the swift_3_oop_chapter_03_13 folder:

    LionSuperHero.averageStrength = 9 
    print(LionSuperHero.averageStrength) 

The following screenshot shows the results of executing the preceding code in the Playground:

Creating values shared by all the instances of a class with type properties

We can easily convert a type property into an immutable type property by replacing the var keyword with the let keyword. For example, we don't want the class users to change the attack power of the superhero group; therefore, we can change the line that declared the attackPower type property with the following line, which creates an immutable type property or a read-only class constant. The code file for the sample is included in the swift_3_oop_chapter_03_14 folder:

public static let attackPower: Int = 10 

The warriorScore type property is a calculated type property that only defines a getter method; therefore, it is a read-only calculated type property. Note that the declaration uses a simplified version of a property, which just has a getter method and simply returns the calculated value after the type (Int):

    public static var warriorScore: Int { 
      return (averageStrength * 3) + (attackPower * 3) + 
      (averageRunningSpeed * 2) + (defensePower * 2) 
    } 

The next lines are equivalent to the previous warriorScore type property declaration. In this case, the declaration uses the get method instead of just returning the calculated value:

    public static var warriorScore: Int { 
      get { 
        return (averageStrength * 3) + (attackPower * 3) + 
        (averageRunningSpeed * 2) + (defensePower * 2) 
      } 
    } 

The following line prints the value for this type property. The code file for the sample is included in the swift_3_oop_chapter_03_14 folder:

    print(LionSuperHero.warriorScore) 

The following lines create a new instance of the LionSuperHero class and use the value of the averageRunningSpeed type property in a sum that specifies the value of the runningSpeedScore argument. The code file for the sample is included in the swift_3_oop_chapter_03_14 folder:

    var superTom = LionSuperHero(name: "Tom", 
    runningSpeedScore: LionSuperHero.averageRunningSpeed + 
    1) 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.98.186