import Swift
// 클래스는 구조체와 유사하다.
// 구조체는 값 타입, 클래스는 참조 타입
class Sample {
var mutableProperty: Int = 100 // 가변 프로퍼티
let immutableProperty: Int = 100 // 불변 프로퍼티
static var typeProperty: Int = 100 // 타입 프로퍼티
// 인스턴스 메서드
func instanceMethod() {
print("instance method")
}
// 타입 메서드
// 재정의 불가 타입 메서드 - static
static func typeMethod() {
print("type method - static")
}
// 재정의 가능 타입 메서드 - class
class func classMethod() {
print("type method - class")
}
}
var mutableReference: Sample = Sample()
mutableReference.mutableProperty = 200
// mutableReference.immutableProperty = 200
let immutableReference: Sample = Sample()
immutableReference.mutableProperty = 200
// immutableReference = mutableReference
// 타입 프로퍼티 및 메서드
Sample.typeProperty = 300
Sample.typeMethod() // type method
//mutableReference.typeProperty = 400
//mutableReference.typeMethod()
class Student {
var name: String = "unknown"
var `class`: String = "Swift"
class func selfIntroduce() {
print("학생타입입니다")
}
func selfIntroduce() {
print ("저는 \(self.class)반 \(name)입니다")
}
}
Student.selfIntroduce() // 학생타입입니다.
var whoamI: Student = Student()
whoamI.name = "whoami"
whoamI.class = "swift"
whoamI.selfIntroduce() // 저는 swift반 whoami입니다
// let 으로 설정해도 변경 가능하다.
let whoareYou: Student = Student()
whoareYou.name = "whoareyou"
whoareYou.selfIntroduce() // 저는 Swift반 whoareyou입니다
'Swift > 기초&문법' 카테고리의 다른 글
스위프트 swift 원하는 이미지 화면 출력 앱 (0) | 2018.08.22 |
---|---|
스위프트 swift 열거형 enum (0) | 2018.08.21 |
스위프트 swift 구조체 (0) | 2018.08.20 |
스위프트 swift 옵셔널 추출 (0) | 2018.08.15 |
스위프트 swift 옵셔널 (0) | 2018.08.14 |