Toy Project/Swift Language Syntax

15. Swift Class Inheritance (상속)

개발자킹콩 2021. 1. 22. 02:51
  • 상속의 규칙
  • A is B -->명제가 논리적으로 성립하면 A는 B를 상속 받을 수 있다.
  • 자식은 한 개의 superclass만 상속받는다.
  • 부모는 여러 자식들을 가질 수 있다.
  • 상속의 깊이는 상관없다.
  • override: 오버라이딩 메소드의 경우 override 키워드를 삽입한다.
  • upper casting: 상위클래스로 casting할 때, 명시적으로 (as 클래스이름) 표시할 수 있다.
    athelete1 = athelete2 as StudentAthlete
    여기서 upper casting가 되었기에 athelete1은 이제 footballTeam에 접근할 수 없다.
  • down casting: 하위클래스로 casting할 때이며, 안전하게 optional하게 casting된다.
    정상적으로 binding가 된 경우 print(~)가 실행된다.
    if let son = athelete1 as? FootballPlayer {
         print("--> team:\(son.footballTeam)")
    }




    import UIKit

    // 처음 주어진 코드
    //struct Grade {
    //    var letter: Character
    //    var points: Double
    //    var credits: Double
    //}
    //
    //class Person {
    //    var firstName: String
    //    var lastName: String
    //
    //    init(firstName: String, lastName: String) {
    //        self.firstName = firstName
    //        self.lastName = lastName
    //    }
    //
    //    func printMyName() {
    //        print("My name is \(firstName) \(lastName)")
    //    }
    //}
    //

//class Student {
//    var grades: [Grade] = []
//
//    var firstName: String
//    var lastName: String
//
//    init(firstName: String, lastName: String) {
//        self.firstName = firstName
//        self.lastName = lastName
//    }
//
//    func printMyName() {
//        print("My name is \(firstName) \(lastName)")
//    }
//}


struct Grade {
    var letter: Character
    var points: Double
    var credits: Double
}

class Person {
    var firstName: String
    var lastName: String


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

    func printMyName() {
        print("My name is \(firstName) \(lastName)")
    }
}

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

 

 


let jay = Person(firstName: "Jay", lastName: "Lee")
let jason = Student(firstName: "Jasson", lastName: "Lee")

jay.firstName
jason.firstName

jay.printMyName()
jason.printMyName()

 

 


let math = Grade(letter: "B", points: 8.5, credits: 3)
let history = Grade(letter: "A", points: 10.0, credits: 3)

jason.grades.append(math)

jason.grades.append(history)

//jay.grades

 

jason.grades.count

 

 

// 학생인데 운동선수
class StudentAthlete: Student {
    var minimumTrainingTime: Int = 2
    var trainedTime: Int = 0
    
    func train() {
        trainedTime += 1
    }
}

// 운동선인데 축구선수
class FootballPlayer: StudentAthlete {
    var footballTeam = "FC Swift"
    
    override func train() {
        trainedTime += 2
    }
}

 

 


 

// Person > Student > Athelete > Football Player

var athelete1 = StudentAthlete(firstName: "Yuna", lastName: "Kim")
var athelete2 = FootballPlayer(firstName: "Heung", lastName: "Son")

athelete1.firstName
athelete2.firstName

 

athelete1.grades.append(math)

athelete2.grades.append(math)

 

athelete1.minimumTrainingTime

athelete2.minimumTrainingTime

 

//athelete1.footballTeam
athelete2.footballTeam

athelete1.train()
athelete1.trainedTime

athelete2.train()
athelete2.trainedTime



athelete1 = athelete2 as StudentAthlete
athelete1.train()
athelete1.trainedTime


if let son = athelete1 as? FootballPlayer {
    print("--> team:\(son.footballTeam)")
}

'Toy Project > Swift Language Syntax' 카테고리의 다른 글

16. Swift Initializer(생성자)  (0) 2021.01.22
14. Swift Class  (0) 2021.01.22
13. Swift Method extension  (0) 2021.01.22
12. Swift Method  (0) 2021.01.22
11. Swift Property(속성)  (0) 2021.01.21