Android第三十六期 - Google官方5.0下拉刷新
代码已经整理好,效果如下: 图一, 图二, 地址:http://down.51cto.com/data/2041626 本文转自 吴雨声 51CTO博客,原文链接:http://blog.51cto.com/liangxiao/1638085,如需转载请自行联系原作者
构造函数作为一种特殊方法,也可以重载。
Swift中构造函数可以多个,他们参数列表和返回值可以不同,这些构造函数构成重载。
示例代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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)"
)
|
构造函数代理
为了减少多个构造函数间的代码重复,在定义构造函数时,可以通过调用其他构造函数来完成实例的部分构造过程,这个过程称为构造函数代理。构造函数代理在结构体和类中使用方式是不同,先介绍结构体中构造函数代理。
将上一节的示例修改如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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)。
将上面的示例修改如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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关东升 51CTO博客,原文链接:http://blog.51cto.com/tonyguan/1747009,如需转载请自行联系原作者
微信关注我们
转载内容版权归作者及来源网站所有!
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。
为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。
Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。
Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。