CLASSES AND OBJECTS

 

//CLASS

//To create a new class use the class keyword followed by the class name and a pair of braces. Inside the braces we can declare the class properties. Notice the capital letter in Monster! Classes MUST be written in capital letter.

class Monster {

    var isAlive = true

    var strength = 10

    

//A Function in a class is called "Method". Whenever you write a method in a class, you use "func" (I know...).

    

    func kill () {

        isAlive = false

    }

}

 

 

//OBJECT

//Instance that is part of the class. The object here is "littleMonster" and it is equal to the class where it belongs.

var littleMonster = Monster ()

print(littleMonster.isAlive)

littleMonster.strength = 25

print(littleMonster.strength)

littleMonster.kill()

print(littleMonster.isAlive)



Here's the updated code: 

class Monster {

    var isAlive = true

    var strength = 10

    func kill () {

        isAlive = false

    }

    func isStrong () -> Bool  {

        if  strength > 10 {

        return true

    } else {

    return false

    }

}

}

var littleMonster = Monster ()

print(littleMonster.isAlive)


littleMonster.strength = 25

print(littleMonster.strength)


littleMonster.kill()

print(littleMonster.isAlive)


print(littleMonster.isStrong())

Happy coding, friends! 🙂