初學swift筆記 方法(九)

 

  1 import Foundation
  2 /*
  3 方法
  4 */
  5 //實例方法 一定需要依附於對象
  6 
  7 class MyPoint {
  8     var x: Double = 0.0
  9     var y: Double = 0.0
 10     //類中的內部方法 第一個參數默認沒有外部參數名,從第二個以後開始,方法名作爲外部參數名 既作爲內部參數又做爲外部參數
 11     //實例方法
 12     func set(_x: Double,_y: Double) {
 13         x = _x
 14         y = _y
 15     }
 16     //實例方法
 17     func show() {
 18         println("x:\(x) y\(y)")
 19     }
 20 }
 21 func set(_x: Double,_y: Double) {
 22     
 23 }
 24 
 25 var p0 = MyPoint()
 26 p0.set(10, _y: 7)
 27 p0.show()
 28 set(10, 10)
 29 
 30 
 31 //結構體中的mutating方法
 32 struct MyPoint_1 {
 33     var x : Double = 0
 34     var y : Double = 0
 35     //結構體和枚舉 值類型不可以直接修改其內的變量值,如果要修改,需要在方法前加關鍵字 mutating
 36     mutating func set(x : Double,y : Double) {
 37         self.x = x
 38         self.y = y
 39         
 40     }
 41     func show() {
 42         println("x:\(self.x) y:\(self.y) ")
 43     }
 44     
 45 }
 46 //枚舉中可以寫方法,但枚舉中不能有存儲屬性,可以有計算屬性
 47 enum LightSwitch {
 48     case OFF,ON,HIGH
 49     mutating func next() {
 50         switch self {
 51         case .OFF:
 52             self = ON
 53         case .ON:
 54             self = OFF
 55         case .HIGH:
 56             self = OFF
 57             
 58         }
 59     }
 60     
 61 }
 62 var p1 = MyPoint_1()
 63 p1.show()
 64 
 65 var light = LightSwitch.OFF
 66 println(light.hashValue)
 67 light.next()
 68 println(light.hashValue)
 69 
 70 /*
 71 類型方法 靜態方法
 72 通過類名+方法名來調用
 73 與static靜態方法相似,該方法爲所有對象共用
 74 */
 75 struct MyPoint_2 {
 76     var p: Int = 0
 77     static var sp: Int = 0
 78     func getvalue() {
 79         println("p:\(p) sp:\(MyPoint_2.sp)")
 80     }
 81     //靜態方法不能夠訪問非靜態變量
 82     static func static_getvalue() {
 83         // println("p:\(p) sp:\(MyPoint_2.sp)")
 84         println("sp:\(MyPoint_2.sp)")
 85 
 86     }
 87     
 88 }
 89 struct MyPoint_3 {
 90     var p: Int = 0
 91     static var sp: Int = 0
 92     func getvalue() {
 93         println("p:\(p) sp:\(MyPoint_3.sp)")
 94     }
 95     //靜態方法不能夠訪問非靜態變量
 96     static func static_getvalue() {
 97         // println("p:\(p) sp:\(MyPoint_3.sp)")
 98         println("sp:\(MyPoint_3.sp)")
 99         
100     }
101     
102 }
103 var m2 = MyPoint_2()
104 m2.getvalue()
105 MyPoint_2.static_getvalue()
106 
107 var m3 = MyPoint_3()
108 m3.getvalue()
109 MyPoint_3.static_getvalue()
110 
111 /*
112 subscripts 下標 訪問對象中數據的快捷方式
113 實例[索引值]
114 */
115 let array = [1,2,3,4,5,6,7]
116 println(array[0]) //實例對象[索引]
117 
118 struct student {
119     var name : String = ""
120     var math : Int
121     var english : Int
122     var chinese : Int
123     func scoreOf(course : String) -> Int? {
124         switch course {
125             case "math":
126             return math
127             case "chinese":
128             return chinese
129             case "english":
130             return english
131         default:
132             return nil
133         }
134     }
135     //製作下標方法
136     /*
137     subscript (course : String) -> Int? {
138         switch course {
139         case "math":
140             return math
141         case "chinese":
142             return chinese
143         case "english":
144             return english
145         default:
146             return nil
147         }
148     }
149     */
150     //或者這麼寫
151     subscript (course : String) -> Int? {
152     get{
153         switch course {
154         case "math":
155             return math
156         case "chinese":
157             return chinese
158         case "english":
159             return english
160         default:
161             return nil
162         }
163     }
164         set{
165             switch course {
166             case "math":
167                  math = newValue!
168             case "chinese":
169                  chinese = newValue!
170             case "english":
171                  english = newValue!
172             default:
173                  println("set error")
174             }
175         }
176     }
177 
178 }
179 var li = student(name: "lisi", math: 90, english: 80, chinese: 98)
180 println(li.scoreOf("math"))
181 println(li["math"])//下標訪問
182 li["math"] = 100 //下標賦值
183 println(li["math"])//下標訪問
184 
185 //下標多索引
186 struct Mul {
187     subscript (a: Int,b: Int) -> Int {
188         return a*b
189     }
190 }
191 var mul = Mul()
192 println(mul[3,6])

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章