Swift_构造过程(指定构造器、convenience便利构造器、可失败构造器 init?(...))

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Jason_chen13/article/details/76827946


class Iland {
    var fish : Double
    init(setFish fish2:Double) {
        fish = 11.3
    }
}


struct Hotal {
    var waiter:Double
    init(waiter2:Double) {
        waiter = waiter2
    }
}



//2
struct Miss {
    let c,d,e:Int
    init(c:Int,d:Int,e:Int) {
        self.c = c
        self.d = d
        self.e = e
    }
}


//3 指定构造器 和 便利构造器
class Food {
    var name: String
    init(name: String) {
        self.name = name
    }
    //便利构造器
    convenience init() {
        self.init(name: "[Unnamed]")
    }

}



//枚举类型构造器
import UIKit

enum Dog {
    case Keep,Log,Min
    
    init?(inChar:Character) {
        switch inChar {
        case "k":
            self = .Keep
        
        case "L":
            self = .Log
            
        case "M":
            self = .Min
        
        default:
            return nil
            
        }
    }
}



class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //1
        var b = Iland(setFish: 2345)
        
        //2
        var t = Hotal(waiter2: 345)
        
        //3
        let f = Food.init()
        
        
        //4
        var d = Dog.init(inChar: "M")
        
    
    
    }


}


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页