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입니다






+ Recent posts