Swift —构造函数与存储属性初始化-备

构造函数的主要作用是初始化实例,其中包括:初始化存储属性和其它的初始化。在Rectangle类或结构体中,如果在构造函数中初始化存储属性width和height后,那么在定义他们时就不需要初始化了。

Rectangle类代码如下:

 

  1. class Rectangle {  
  2.   
  3.     var  Double  
  4.   
  5.     var height: Double  
  6.   
  7.       
  8.   
  9.     init() {  
  10.   
  11.         width   = 0.0  
  12.   
  13.         height  = 0.0  
  14.   
  15.     }  
  16.   
  17. }  

如果存储属性在构造函数中没有初始化,在定义的时候也没有初始化,那么就会发生编译错误。

构造函数还可以初始化常量存储属性,下面我们看示例代码:

 

  1. class Employee {           
  2.   
  3.     let no: Int               //常量类型属性。曾讲过常量只能在定义的同时赋值,而在构造函数中,常量属性可以不遵守这个规则  
  4.   
  5.     var name: String?     //存储属性一般在定义的时候初始化。如果不能确定初始值,可以采用可选类型属性  
  6.   
  7.     var job: String?      //存储属性  
  8.   
  9.     var salary: Double          
  10.   
  11.     var dept: Department? //存储属性  
  12.   
  13.       
  14.   
  15.     init() {                
  16.   
  17.         no = 0                 //在构造函数中赋值,这种赋值不能放在普通方法中  
  18.   
  19.         salary = 0.0         
  20.   
  21.         dept = nil           
  22.   
  23.     }  
  24.   
  25. }  
  26.   
  27.    
  28.   
  29. struct Department {        
  30.   
  31.     let no: Int               //常量类型属性  
  32.   
  33.     var name: String        
  34.   
  35.    
  36.   
  37.     init() {                
  38.   
  39.         no = 10            //在构造函数中赋值,这种赋值不能放在普通方法中  
  40.   
  41.         name = "SALES"       
  42.   
  43.     }  
  44.   
  45. }  
  46.   
  47.    
  48.   
  49. let dept = Department()  
  50.   
  51. var emp = Employee()  

 

使用外部参数名

为了增强程序的可读性,Swift中的方法和函数可以使用外部参数名。在构造函数中也可以使用外部参数名。构造函数中的外部参数名要比一般的方法和函数更有意义,由于构造函数命名都是init,如果一个类型中有多个构造函数,我们可以通过不同的外部参数名区分调用不同的构造函数。

下面看示例代码:

 

  1. class RectangleA {  
  2.   
  3.     var  Double  
  4.   
  5.     var height: Double  
  6.   
  7.       
  8.   
  9.     init(W  Double,H height: Double) {  //定义构造函数有两个参数width和height,并且为参数提供了外部参数名W和H。  
  10.   
  11.         self.width   = width        //函数参数赋值给属性  
  12.   
  13.         self.height  = height  //  
  14.   
  15.     }  
  16.   
  17. }  
  18.   
  19.    
  20.   
  21. var recta = RectangleA(W: 320, H: 480)       //创建RectangleA实例,这里使用了外部参数名  
  22.   
  23. print("长方形A:(recta.width) x (recta.height)")  

这里我们定义的是类,但也完全适用于结构体。

构造函数中的局部参数名可以直接作为外部参数名使用。

下面看示例代码:

 

    1. class RectangleB {  
    2.   
    3.     var  Double  
    4.   
    5.     var height: Double  
    6.   
    7.       
    8.   
    9.     init( Double, height: Double) { //构造函数,其中没有声明外部参数名。  
    10.   
    11.         self.width   = width  
    12.   
    13.         self.height  = height  
    14.   
    15.     }  
    16.   
    17. }  
    18.   
    19.    
    20.   
    21. var rectb = RectangleB( 320, height: 480) //代码调用构造函数时,我们使用了外部参数名width和height,这些外部参数名就是局部参数名  
    22.   
    23. print("长方形B:(rectb.width) x (rectb.height)")  

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

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

示例代码如下:

 

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

 

构造函数代理

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

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

 

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

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

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

 

类构造函数横向代理

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

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

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

将上面的示例修改如下:

 

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

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

原文地址:https://www.cnblogs.com/isItOk/p/5454161.html