Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swift] is연산자 #50

Closed
seungchan2 opened this issue May 18, 2022 · 0 comments
Closed

[Swift] is연산자 #50

seungchan2 opened this issue May 18, 2022 · 0 comments
Assignees
Labels

Comments

@seungchan2
Copy link
Owner

seungchan2 commented May 18, 2022

타입캐스팅 is연산자 as연산자 를 알아보겠음

클래스 부분에서 두 키워드를 사용함

왜냐면 클래스는 상속이 가능하기 때문임

클래스의 상속에 관해 간단한 예시를 살펴보겠음

여기서 상속은 저장 프로퍼티를 추가하는 관점에서 보겠음

class Person {
    var id = 0
    var name = "이름"
    var email = "abc@gmail.com"
}

class Student: Person {
    var studentId = 1
}

class Undergraduate: Student {
    var major = "전공"
}
let person1 = Person()
person1.id
person1.name
person1.email

let student1 = Student()
student1.id
student1.name
student1.email
student1.studentId

let undergraduate1 = Undergraduate()
undergraduate1.id
undergraduate1.name
undergraduate1.email
undergraduate1.studentId
undergraduate1.major   

이런 식으로 인스턴스를 생성하고 프로퍼티에 접근이 가능해짐

is 연산자

is 연산자는 타입에 대한 검사를 수행하는 연산자임

True 아니면 False 밖에 나오지 않을 거임

위에서 만든 인스턴스에 빗대어 예시를 살펴보겠음

person1 is Person                // true
person1 is Student               // false
person1 is Undergraduate         // false

student1 is Person               // true
student1 is Student              // true
student1 is Undergraduate        // false

undergraduate1 is Person         // true
undergraduate1 is Student        // true
undergraduate1 is Undergraduate  // true

이해가 감?

상속의 구조를 이해하고 살펴보면 쉬움

스크린샷 2022-05-10 오후 8 10 42

위의 그림을 보면 Undergraduate() 는 Student, Person의 더 큰 범위임

ㅇㅋ 여기까지임

@seungchan2 seungchan2 self-assigned this May 18, 2022
@seungchan2 seungchan2 changed the title [Swift] is연산자 / as연산자 [Swift] is연산자 May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant