Ray Wenderlich Functions and Types Course - Part 10

Last chapter! The course suddenly got a lot harder with properties and methods so hopefully this will be accessible. Then it will be the consolidation one!

Start Time - 17:47

Inheritance

For classes, inheritance is something I've used before.

class Person {
  var firstName: String
  var lastName: String
  
  init(firstName: String, lastName: String) {
    self.firstName = firstName
    self.lastName = lastName
  }
}

class Student: Person {
  var grades: [Grade] = []
  
  
}


let bob = Student(firstName: "Bob", lastName: "Stevens")

All of that is fine. The super and sub class etc.

Person is a base class and student inherits from that. Person could have as many sub classes as necessary. You can subclass from a subclass.

Paused for approx 25 minutes.

Polymorphism - a subclass can be treated as its own type or one of its superclasses.

This links to casting too - up and down casting. Use of the as keyword.

Examples about casting -

let dude2 = dude as! StudentAthlete

func getActivity(student: Student) -> String {
    
    if let athleteMember = student as? StudentAthlete {
        return " \(athleteMember) is practising for the team!"
    } else {
        return "Not eligible"
    }
}


getActivity(student: dude)

Also, there is making computed property with override. 

Challenge!

Paused at 18:50; restart at 20:47

Finished challenges! Actually I think I've smashed them...let's see!

Finished at 20:57 (total time approx 45 minutes)

So a good session. All about inheritance with some computed properties and other concepts brought back in. The fact that I did the challenge without too many difficulties is a really good sign! More tomorrow! 

Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)