Extension


Subscript


ex1


class School {
    var number: Int = 0
    var students: [Student] = [Student] ()
    
    func addStudent(name: String){
        let student: Student = Student(name:name, number: self.number)
        self.students.append(student)
        self.number += 1
    }
    
    func addStudents(names: String ...) {
        for name in names {
            self.addStudent(name: name)
        }
    }
    //index가 학생수보다 작으면 학생 배열의 값을 넘겨준다.
    subscript(index: Int) -> Student? {
        get{
                if index < self.number {
                    return self.students[index]
                }
                return nil
            }
        
        set{
            guard var newStudent: Student = newValue else { return }
            
            var number: Int = index
            if number > self.number {
                number = self.number
                self.number += 1
            }
            
            newStudent.number = number
            self.students.append(newStudent)
        
        }
    }
}

let highSchool: School = School()
highSchool.addStudents(names: "a", "b", "c")

//3이라는 번호에 학생을 할당
highSchool[3] = Student(name: "ohayo", number: 0)

let aStudent: Student? = highSchool[1]
print("\(aStudent?.number) \(aStudent?.name)")
Optional("a")




inheritance


ex1

class name: 상위클래스 {
}


ex2

class Person {
    var name: String = ""
    var age: Int = 0
    var introduction: String {
        return "name: \(name) age:\(age)"
    }
    
    func speak() {
        print("abcddd")
    }
}

class Student: Person {
    
    var grade: String = "F"
    func study() {
        print("Study hard")
    }
    
}

let pk: Person = Person()
pk.name = "pk"
pk.age = 10

let pkStudent: Student = Student()
pkStudent.speak()



Override


ex1

class Person {
    var name: String = ""
    var age: Int = 0
    var introduction: String {
        return "name: \(name) age:\(age)"
    }
    
    func speak() {
        print("abcddd")
    }
}

class Student: Person {
    
    var grade: String = "F"
    func study() {
        print("Study hard")
    }
    override func speak() {
        super.speak()
        print("override speack abcddd")
    }
    
}

let pk: Person = Person()
pk.name = "pk"
pk.age = 10

let pkStudent: Student = Student()
pkStudent.speak()


ex2

class Person {
    var name: String = ""
    var age: Int = 0
    var introduction: String {
        return "name: \(name) age:\(age)"
    }
    
    func speak() {
        print("abcddd")
    }
}

class Student: Person {
    
    var grade: String = "F"
    func study() {
        print("Study hard")
    }
    override func speak() {
        super.speak()
        print("override speack abcddd")
    }
    override var introduction: String {
        return super.introduction + "" + "학점 : \(self.grade)"
    }
    
}

let pk: Person = Person()
pk.name = "pk"
pk.age = 10

let pkStudent: Student = Student()
pkStudent.speak()

pkStudent.introduction


ex3


struct Student {
    var name: String
    var number: Int
}


class School {
    var students: [Student]    = [Student]()
    subscript (index: Int) -> Student {
        return students[index]
    }
}

class MiddleScholl: School {
    var middleStudents: [Student] = [Student]()
    override subscript (index:Int) -> Student {
        print("overred")
        return middleStudents[index]
    }
}


var middleSchool: MiddleScholl = MiddleScholl()
middleSchool.middleStudents = [Student(name: "a",number: 10),Student(name: "a",number: 20)]
print(middleSchool[0])


ex4

final class Student: Person {
//error!!! final로 막았기 때문
	override var name: String ..
}



마치며



참고 & 출처 스위프트 프로그래밍