Swift面向对象之枚举

前端之家收集整理的这篇文章主要介绍了Swift面向对象之枚举前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. //
  2. // main.swift
  3. // Swift面向对象之枚举
  4. //
  5. // Created by Goddog on 15/7/17.
  6. // Copyright © 2015年 Goddog. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10.  
  11. print("Swift不仅是面向过程还是面向对象的,Swift可以定义枚举、结构体、类3种面向对象的类型,用枚举、结构体定义的变量属于值类型,用类定义的变量属于引用类型,3种面向对象都支持定义存储属性、计算属性方法、下标、构造器和嵌套类型")
  12.  
  13. print("Swift5种面向对象的程序单元:类、结构体、枚举、扩展、协议")
  14.  
  15. print("定义类型的步骤:1.声明变量;2.创建实例;3.访问属性;4.调用方法;5.派生子类(枚举和结构体不支持继承)")
  16.  
  17. //MARK: - 定义枚举
  18. enum Season
  19. {
  20. case Spring
  21. case Summer
  22. case Fall
  23. case Winter
  24. }
  25. //或
  26. enum Weekday
  27. {
  28. case Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday
  29. }
  30. //使用枚举声明变量
  31. var day: Weekday
  32. //使用已有枚举赋值
  33. day = Weekday.Saturday
  34. print("结果是:\(day)")
  35.  
  36. //MARK: - 枚举值和switch语句
  37. var chooseDay = Weekday.Monday
  38. switch chooseDay
  39. {
  40. case .Monday:
  41. print("周一")
  42. case .Tuesday:
  43. print("周二")
  44. case .Wednesday:
  45. print("周三")
  46. case .Thursday:
  47. print("周四")
  48. case .Friday:
  49. print("周五")
  50. case .Saturday:
  51. print("周六")
  52. default:
  53. print("周日")
  54. }
  55.  
  56. //MARK: - 原始值
  57. //定义枚举,指定原始值为Int
  58. enum Weekdays: Int
  59. {
  60. case Monday,Tuesday = 1,Wednessday = 5,Sunnday
  61. }
  62. var days = Weekdays.Monday
  63. print("Monday is : \(days.rawValue)") //0是根据Tuesday=1自动判断的
  64. days = .Friday
  65. print("Friday is : \(days.rawValue)") //7是根据Wednessday=5自动判断的
  66.  
  67. enum Seasons: Character
  68. {
  69. //为每个枚举指定原始值
  70. case Spring = "s"
  71. case Summer = "u"
  72. case Fall = "f"
  73. case Winter = "w"
  74. }
  75.  
  76. //根据原始值获取枚举
  77. var getSeason = Seasons(rawValue: "s")
  78. if getSeason != nil
  79. {
  80. switch (getSeason!)
  81. {
  82. case .Spring:
  83. print("春")
  84. case .Summer:
  85. print("夏")
  86. case .Fall:
  87. print("秋")
  88. default:
  89. print("冬")
  90. }
  91. }
  92.  
  93.  
  94. //MARK: - 关联值
  95. enum Planet
  96. {
  97. //不同枚举可以携带数量不等,类型不等的关联值
  98. case Mercury(weight: Double,density: Double,name: String)
  99. case Venus(Double,Double)
  100. case Earth(Double,String)
  101. case Mars(density: Double,name:String)
  102. case Jupiter
  103. case Saturn
  104. case Uranus
  105. case Nepturn
  106. }
  107.  
  108. var p1 = Planet.Mercury(weight: 0.05,density: 5.43,name: "水星")
  109. var p2 = Planet.Venus(0.815,5.25)
  110. var p3 = Planet.Earth(1.0,"地球")
  111. var p4 = Planet.Mars(density: 3.95,name: "火星")
  112.  
  113. switch(p3)
  114. {
  115. case Planet.Earth(var weight,var name):
  116. print("\(name),\(weight)")
  117. default:
  118. break
  119. }
  120.  
  121. switch (p1)
  122. {
  123. case let .Venus(weight,density):
  124. print("\(weight),\(density)")
  125. case let .Mercury(weight: w,density: d,name: n):
  126. print("\(w),\(d),\(n)")
  127. default:
  128. break
  129. }
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  1. Swift不仅是面向过程还是面向对象的,Swift可以定义枚举、结构体、类3种面向对象的类型,用枚举、结构体定义的变量属于值类型,用类定义的变量属于引用类型,3种面向对象都支持定义存储属性、计算属性方法、下标、构造器和嵌套类型
  2. Swift5种面向对象的程序单元:类、结构体、枚举、扩展、协议
  3. 定义类型的步骤:1.声明变量;2.创建实例;3.访问属性;4.调用方法;5.派生子类(枚举和结构体不支持继承)
  4. 结果是:Swift面向对象之一.Weekday.Saturday
  5. 周一
  6. Monday is : 0
  7. Friday is : 7
  8. 地球,1.0
  9. 0.05,5.43,水星
  10. Program ended with exit code: 0

猜你在找的Swift相关文章