zoukankan      html  css  js  c++  java
  • Swift 2 语言精要

    init相当于构造函数

    deinit相当于析构函数

    class InitAndDeinitExample {
        // Designated (i.e., main) initializer
        init () {
            print("I've been created!")
        }
    
        // Convenience initializer, required to call the
        // designated initializer (above)
        convenience init (text: String) {
            self.init() // this is mandatory
            print("I was called with the convenience initializer!")
        }
        // Deinitializer
        deinit {
            print("I'm going away!")
        }
    }

    调用的例子:

    var example : InitAndDeinitExample?
    
    // using the designated initializer
    example = InitAndDeinitExample() // prints "I've been created!"
    example = nil // prints "I'm going away"
    
    // using the convenience initializer
    example = InitAndDeinitExample(text: "Hello")

    运行结果:

    I've been created!
    I was called with the convenience initializer

  • 相关阅读:
    类的继承
    垃圾回收GC
    网络层
    数据链路层
    TCP/IP协议分层模型
    OSI参考模型
    浏览器访问一个域名的过程
    Thread&ThreadLocal
    设计模式---单例模式
    内存泄漏和内存溢出
  • 原文地址:https://www.cnblogs.com/davidgu/p/5328267.html
Copyright © 2011-2022 走看看