Making an instance of the class

Once you have a class declaration, you can use it to create instances of that class. You will now create an instance of the Animal class that represents a cat. Do the following steps:

  1. Type the following after your class declaration and run it:
// Making an instance of the class
let cat = Animal()

// Printing out the property values
print(cat.name)
print(cat.sound)
print(cat.numberOfLegs)
print(cat.breathesOxygen)

// Calling an instance method
cat.makeSound()

You access the instance properties and methods by typing a dot after the instance name, followed by the property or method you want. You will see that the values for all of the instance properties will be listed in the Debug area. Since the values are the default values assigned when the class was created, you'll see name and sound are empty strings, numberOfLegs is 0, and breathesOxygen is true. After the cat.makeSound() method is called, an empty string is printed in the Debug area.

Let's assign some values to this instance's properties.

  1. Modify your code as shown:
// Making an instance of the class
let cat = Animal()

// Assigning some values to the properties in the instance
cat.name = "Cat"
cat.sound = "Mew"
cat.numberOfLegs = 4
cat.breathesOxygen = true

// Printing out the property values
print(cat.name)
print(cat.sound)
print(cat.numberOfLegs)
print(cat.breathesOxygen)

// Calling an instance method
cat.makeSound()

That's better. Now, when you run the program, the following is displayed in the Debug area:

Cat
Mew
4
true
Mew

Note that we have to create an instance and, after that, assign the values. Is it possible to assign the values when the instance is created? Indeed, it is, and you do this by implementing an initializer in your class declaration. An initializer is responsible for ensuring all of the instance properties have valid values when a class is created.

  1. Modify your class declaration as shown:
// Classes
// Creating a class declaration
class Animal {
var name: String
var sound: String
var numberOfLegs: Int
var breathesOxygen: Bool = true

// Class initializer
init(name: String, sound: String, numberOfLegs: Int,
breathesOxygen: Bool) {
self.name = name
self.sound = sound
self.numberOfLegs = numberOfLegs
self.breathesOxygen = breathesOxygen
}

func makeSound() {
print(self.sound)
}
}

As you can see, an initializer starts with the init keyword and has a list of parameters that will be used to set the property values. Note the use of the self keyword to distinguish the property names from the parameters, for instance, self.name refers to the property and name refers to the parameter. At the end of the initialization process, every property in the class should have a value.

You'll see some errors in your code at this point. You will need to update your function call to address this.

  1. Modify your code as shown and run it:
// Making an instance of the class
let cat = Animal(name: "Cat", sound: "Mew", numberOfLegs: 4, breathesOxygen: true)

// Printing out the property values
print(cat.name)
print(cat.sound)
print(cat.numberOfLegs)
print(cat.breathesOxygen)

// Calling an instance method
cat.makeSound()

The results are the same as before, but you created the instance and set its properties in a single step. Excellent!

Now, there are different types of animals, such as mammals, birds, reptiles, and fish. You could create a class for each type, but you could also create a subclass based on an existing class. Let's see how to do that.

..................Content has been hidden....................

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