페이지

2017년 2월 18일 토요일

Swift Instance Method, Type Method,subscript

struct StructSample{
    static func typeMethod(){
        print("i am type method")
    }
    func instanceMethod(){
        print("i am instance method")
    }
}

var s = StructSample()
s.instanceMethod()
StructSample.typeMethod()

class ClassSample{
    //class keyword allow subclasses to override superclass implements
    class func typeMethod(){
        print("i am type method")
    }

    //static keyword not allow subclasses to override superclass implements 
    static func staticTypeMethod(){
        print(" static method from statcTypeMechod")
    }

    func instanceMethod(){
        print("i am instance method")
    }
}
var c = ClassSample()
c.instanceMethod()
ClassSample.typeMethod()


//static, class modifiers ?
class SomeClass {
    class var overrideType: Int {
        get {
            return 107
        }
    }
    static var type2 : Int  {
        get {
            return 1
        }
    }
}

print(SomeClass.overrideType)
print(SomeClass.type2)

//subscript
struct SubscriptSample{
    var weight : Int
    subscript(index:Int) -> Int {
        return index * weight
    }
}

var s = SubscriptSample(weight:3)
print( s[2],s[3])

댓글 없음:

댓글 쓰기