《从零开始学Swift》学习笔记(Day 39)——构造函数重载
admin
2023-02-10 06:40:08
0


原创文章,欢迎转载。转载请注明:关东升的博客

 

构造函数作为一种特殊方法,也可以重载。

Swift中构造函数可以多个,他们参数列表和返回值可以不同,这些构造函数构成重载。

示例代码如下:

class Rectangle {
    
    var width: Double
    var height: Double
    
    init(width: Double, height: Double) {
        self.width   = width
        self.height  = height
    }
    
    init(W width: Double,H height: Double){  
        self.width   = width
        self.height  = height
    }
    
    init(length: Double) {    
        self.width   = length
        self.height  = length
    }
 
    init() {  
        self.width   = 640.0
        self.height  = 940.0
    }
 
}
 
var rectc1 =Rectangle(width: 320.0, height: 480.0)  
print("长方形:\(rectc1.width) x\(rectc1.height)")
 
var rectc2 = Rectangle(W: 320.0, H: 480.0)           
print("长方形:\(rectc2.width) x\(rectc2.height)")
 
var rectc3 =Rectangle(length: 500.0)                
print("长方形3:\(rectc3.width) x\(rectc3.height)")
 
var rectc4 = Rectangle()                        
print("长方形4:\(rectc4.width) x\(rectc4.height)")


 

构造函数代理

为了减少多个构造函数间的代码重复,在定义构造函数时,可以通过调用其他构造函数来完成实例的部分构造过程,这个过程称为构造函数代理。构造函数代理在结构体和类中使用方式是不同,先介绍结构体中构造函数代理。

将上一节的示例修改如下:

struct Rectangle {
 
    var width: Double
    var height: Double
    
    init(width: Double, height: Double) {
        self.width   = width
        self.height  = height
    }
    
    init(W width: Double,H height: Double){  
        self.width   = width
        self.height  = height
    }
    
    init(length: Double) {                    //调用了self.init语句
        self.init(W: length, H: length)
    }
 
    init() {                              //调用了self.init语句
        self.init(width: 640.0, height: 940.0)
    }
 
}
 
var rectc1 =Rectangle(width: 320.0, height: 480.0)  
print("长方形:\(rectc1.width) x\(rectc1.height)")
 
var rectc2 = Rectangle(W: 320.0, H: 480.0)           
print("长方形:\(rectc2.width) x\(rectc2.height)")
 
var rectc3 =Rectangle(length: 500.0)                
print("长方形3:\(rectc3.width) x\(rectc3.height)")
 
var rectc4 = Rectangle()                        
print("长方形4:\(rectc4.width) x \(rectc4.height)")


Rectangle声明为结构体类型,其中也有4个构造函数重载。

这种在同一个类型中通过self.init语句进行调用当前类型其它构造函数,其它构造函数被称为构造函数代理。

 

类构造函数横向代理

由于类有继承关系,类构造函数代理比较复杂,分为横向代理和向上代理。

  • 横向代理类似于结构体类型构造函数代理,发生在同一类内部,这种构造函数称为便利构造函数convenience initializers)。

  • 向上代理发生在继承情况下,在子类构造过程中要先调用父类构造函数,初始化父类的存储属性,这种构造函数称为指定构造函数designated initializers)。

将上面的示例修改如下:

class Rectangle {
 
    var width: Double
    var height: Double
    
    init(width: Double, height: Double){    
        self.width   = width
        self.height  = height
    }
    
    init(W width: Double,H height: Double){      
        self.width   = width
        self.height  = height
    }
    
    convenience init(length: Double) {            
        self.init(W: length, H: length)
    }
 
    convenience init() {                      
        self.init(width: 640.0, height: 940.0)
    }
 
}
 
var rectc1 =Rectangle(width: 320.0, height: 480.0)  
print("长方形:\(rectc1.width) x\(rectc1.height)")
 
var rectc2 = Rectangle(W: 320.0, H: 480.0)           
print("长方形:\(rectc2.width) x\(rectc2.height)")
 
var rectc3 =Rectangle(length: 500.0)                
print("长方形3:\(rectc3.width) x\(rectc3.height)")
 
var rectc4 = Rectangle()                        
print("长方形4:\(rectc4.width) x\(rectc4.height)")


Rectangle声明为类,其中也有4个构造函数重载。

 

欢迎关注关东升新浪微博@tony_关东升。
关注智捷课堂微信公共平台,了解最新技术文章、图书、教程信息
                              《从零开始学Swift》学习笔记(Day 39)——构造函数重载

更多精品iOSCocos、移动设计课程请关注智捷课堂官方网站:http://www.zhijieketang.com
智捷课堂论坛网站:http://51work6.com/forum.php

 

 


相关内容

热门资讯

晒出317万年终奖后,腾讯一员... 近日,有网友晒出疑似腾讯内部发布的通报:WXG某项目组负责人叶某在职期间,因私自泄露公司敏感信息,被...
公安部:国际打击电信网络诈骗联... 今天(24日)上午,国务院新闻办公室举行“开局起步‘十五五’”系列主题新闻发布会,公安部相关负责人介...
数学之外的邓煜:INTJ、知乎... 在23日于美国费城宾夕法尼亚州会展中心举行的2026年国际数学家大会开幕式上,中国数学家邓煜、王虹获...
新晋菲尔兹奖得主,当天宣布加入... 刚拿到数学界最高荣誉,转头就宣布:我要去OpenAI了。Jacob Tsimerman,这位领奖台上...
竞逐百亿赛道,北京十条政策卡位... 从基础模型到框架性能、推理效率、行业应用,半年来北京以智能体为牵引不断精进。作为我国AI发展的引领者...
共促数智交融 共启丝路新篇—... 本报记者 亓玉昆7月22日,2026年世界互联网大会数字丝路发展论坛在陕西西安举行。论坛以“智汇丝路...
赛里木湖景区通报“工作人员殴打... 2026年7月23日17时许,赛里木湖景区入口处发生多名景区工作人员殴打旅游车司机事件,我们深感痛心...
继英伟达黄仁勋后,AMD苏姿丰... IT之家 7 月 24 日消息,随着中国开源 AI 模型的不断变强,近期美国业界出现了关于 AI 是...
辰奕智能获得实用新型专利授权:... 证券之星消息,根据天眼查APP数据显示辰奕智能(301578)新获得一项实用新型专利授权,专利名为“...
湖南湘江新区文化创意产业出海观... 中新网长沙7月23日电(胡语桐)在日本大型量贩门店的货架上,中国研发的视频创意软件实体产品陈列其中;...