swift 类方法(+)与实例方法(-)

跟OC一样,swift方法也分为实例方法(-)与类方法(+),然后说下在swift中实例方法与类方法的实现

1.实例方法

就是只能用对象实例调用的方法,也可以称为“对象方法”,与函数语法一样

class Dog {

    func run() {

        print("run")

    }

}

var d = Dog()

//对象名调用

d.run()

2.类方法

直接用类调用类型方法,不能用对象调用类型方法,相比swift中的实例方法,用class修饰

class Dog {

   class func run() {

        print("run")

    }

}

//类名调用

Dog.run()







猜你喜欢

转载自www.cnblogs.com/cui-cui/p/9953384.html
今日推荐