Swift 2.0学习笔记(Day 39)——构造函数重载

前端之家收集整理的这篇文章主要介绍了Swift 2.0学习笔记(Day 39)——构造函数重载前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
原创文章,欢迎转载。转载请注明:关东升的博客

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

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

示例代码如下:

  1. class Rectangle {
  2.  
  3.  
  4. var width: Double
  5.  
  6. var height: Double
  7.  
  8.  
  9. init(width: Double,height: Double) {
  10.  
  11. self.width = width
  12.  
  13. self.height = height
  14.  
  15. }
  16.  
  17.  
  18. init(W width: Double,H height: Double) {
  19.  
  20. self.width = width
  21.  
  22. self.height = height
  23.  
  24. }
  25.  
  26.  
  27. init(length: Double) {
  28.  
  29. self.width = length
  30.  
  31. self.height = length
  32.  
  33. }
  34.  
  35.  
  36. init() {
  37.  
  38. self.width = 640.0
  39.  
  40. self.height = 940.0
  41.  
  42. }
  43.  
  44.  
  45. }
  46.  
  47.  
  48. var rectc1 = Rectangle(width: 320.0,height: 480.0)
  49.  
  50. print("长方形:\(rectc1.width) x \(rectc1.height)")
  51.  
  52.  
  53. var rectc2 = Rectangle(W: 320.0,H: 480.0)
  54.  
  55. print("长方形:\(rectc2.width) x \(rectc2.height)")
  56.  
  57.  
  58. var rectc3 = Rectangle(length: 500.0)
  59.  
  60. print("长方形3:\(rectc3.width) x \(rectc3.height)")
  61.  
  62.  
  63. var rectc4 = Rectangle()
  64.  
  65. print("长方形4:\(rectc4.width) x \(rectc4.height)")

构造函数代理

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

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

  1. struct Rectangle {
  2.  
  3.  
  4. var width: Double
  5.  
  6. var height: Double
  7.  
  8.  
  9. init(width: Double,H height: Double) {
  10.  
  11. self.width = width
  12.  
  13. self.height = height
  14.  
  15. }
  16.  
  17.  
  18. init(length: Double) { //调用了self.init语句
  19.  
  20. self.init(W: length,H: length)
  21.  
  22. }
  23.  
  24.  
  25. init() { //调用了self.init语句
  26.  
  27. self.init(width: 640.0,height: 940.0)
  28.  
  29. }
  30.  
  31.  
  32. }
  33.  
  34.  
  35. var rectc1 = Rectangle(width: 320.0,H: 480.0)
  36.  
  37. print("长方形:\(rectc2.width) x \(rectc2.height)")
  38.  
  39.  
  40. var rectc3 = Rectangle(length: 500.0)
  41.  
  42. print("长方形3:\(rectc3.width) x \(rectc3.height)")
  43.  
  44.  
  45. var rectc4 = Rectangle()
  46.  
  47. print("长方形4:\(rectc4.width) x \(rectc4.height)")

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

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

类构造函数横向代理

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

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

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

将上面的示例修改如下:

  1. class Rectangle {
  2.  
  3.  
  4. var width: Double
  5.  
  6. var height: Double
  7.  
  8.  
  9. init(width: Double,height: Double) {
  10.  
  11. self.width = width
  12.  
  13. self.height = height
  14.  
  15. }
  16.  
  17.  
  18. init(W width: Double,H height: Double) {
  19.  
  20. self.width = width
  21.  
  22. self.height = height
  23.  
  24. }
  25.  
  26.  
  27. convenience init(length: Double) {
  28.  
  29. self.init(W: length,H: length)
  30.  
  31. }
  32.  
  33.  
  34. convenience init() {
  35.  
  36. self.init(width: 640.0,H: 480.0)
  37.  
  38. print("长方形:\(rectc2.width) x \(rectc2.height)")
  39.  
  40.  
  41. var rectc3 = Rectangle(length: 500.0)
  42.  
  43. print("长方形3:\(rectc3.width) x \(rectc3.height)")
  44.  
  45.  
  46. var rectc4 = Rectangle()
  47.  
  48. print("长方形4:\(rectc4.width) x \(rectc4.height)")

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

欢迎关注关东升新浪微博@tony_关东升。
关注智捷课堂微信公共平台,了解最新技术文章、图书、教程信息
更多精品iOSCocos、移动设计课程请关注智捷课堂官方网站:http://www.zhijieketang.com智捷课堂论坛网站:http://51work6.com/forum.php

猜你在找的Swift相关文章