Review Class and Inheritance. Let's Struck!
== Let's review class, super class, sub-class ==
class Vehicle {
var type: String
var wheel: Int
var color: String
var door: Int
init() {
type = "Default Car"
wheel = 4
color = "Default Blue"
door = 4
}
func description() -> String {
return "I am a \(color) \(type). I have \(wheel) wheels and \(door) doors."
}
}
class Truck: Vehicle {
override init() {
super.init()
type = "Car"
color = "Green"
door = 5
}
}
print(Truck().description())
== Value Type vs Reference Type (Classes) ==
Value type (different memory location)
var myAge = 4
var yourAge = myAge
yourAge = 15
print("myAge is \(myAge) and yourAge is \(yourAge)")
Reference Type (same memory location) - change 1 value, the other value will also change
class Kids {
var age: Int
init(age: Int) {
self.age = age
}
}
var megan = Kids(age: 4)
var natalie = megan
natalie.age = 12
print("Megan age is \(megan.age) and Natalie age is \(natalie.age)")
== Struct ==
1. Value type (unlike class is reference type)
2. Struct does not have inheritance
struct Couch {
var numberOfCushions = 4
func description() -> String {
return "I have \(numberOfCushions) cushions."
}
}
var myFirstCouch = Couch()
var mySecondCouch = myFirstCouch
mySecondCouch.numberOfCushions = 5
print(myFirstCouch.description())
print(mySecondCouch.description())
==== Homework ===
- Review all lessons of Swift
- Connect with RollingCoders Google Hangout if have questions
- Finish watching the video