swift-重写init方法

class BaseVC: UIViewController {
    init(){
        super.init(nibName: nil, bundle: nil)
        self.view.backgroundColor = UIColor.red
    }
    init(name : String ,age : Int) {
        super.init(nibName: nil, bundle: nil)
        self.view.backgroundColor = UIColor.blue
        print(name,age);
    }
    convenience init(name : String) {
        self.init(name: "豆豆", age: 1)
        self.view.backgroundColor = UIColor.orange
        print(name);
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}


//调用方式
    var vc : BaseVC = {
        var base1 = BaseVC()
        var base2 = BaseVC.init(name: "豆豆", age: 18)
        var base3 = BaseVC.init(name: "豆豆")
        return base1;
    }();

 

你可能感兴趣的:(iOS知识点)