标签:
1 // Playground - noun: a place where people can play 2 3 import UIKit 4 5 6 //使用关键字class和struct来分别表示类和结构体 7 8 struct SomeStruct { 9 10 } 11 12 class SomeClass { 13 14 } 15 16 17 struct Length { 18 //在swift中可以对变量初始化 19 var start: Float = 0.0 20 var end: Float = 0.0 21 22 //在swift中可以定义方法 23 func calLength () { 24 println(end - start) 25 } 26 } 27 28 29 //调用默认的构造器 init 30 var length = Length() 31 length.start = 10 32 length.end = 20 33 34 length.calLength() 35 36 //逐一构造器,只有结构体才有,类是没有的 37 var length2 = Length(start: 2, end: 15) 38 length2.calLength() 39 40 41 //定义类 42 class Car { 43 var name: String? 44 45 func printName() -> String { 46 return name! 47 } 48 } 49 50 var car = Car() 51 car.name = "法拉利??" 52 car.printName() 53 54 55 // 权限控制 56 // public 公共的 57 // private 私有的 58 // internal 内部的 59 60 61 //TODO 62 class Permission { 63 var guozhong: String? 64 private var qiaodaye: String? 65 internal var xiangwang: String? 66 } 67 68 var permission = Permission() 69 permission.guozhong = "国中" 70 permission.qiaodaye = "乔大爷" 71 permission.xiangwang = "祥望" 72 73 74 //构造器 75 class ZS { 76 var name: String? 77 78 //默认构造器 79 init() { 80 name = "张帅" 81 } 82 83 //自定义构造器 84 init(name: String) { 85 self.name = name 86 } 87 88 //析构方法 89 deinit { 90 91 } 92 } 93 94 var zs = ZS() 95 zs.name! 96 97 var zs1 = ZS(name: "张帅2") 98 zs1.name! 99 100 101 //属性 102 //1.存储属性 103 struct Point { 104 var x = 0.0 105 var y = 0.0 106 } 107 108 struct Size { 109 var width = 0.0 110 var height = 0.0 111 } 112 113 //2.计算类型,不直接存值,提供了一个getter方法来获取值,提供了一个setter方法来设置新的值 114 struct Rect { 115 var origin = Point() 116 var size = Size() 117 var center: Point { 118 get { 119 let centerX = self.origin.x + self.size.width / 2 120 let centerY = self.origin.y + self.size.height / 2 121 return Point(x: centerX, y: centerY) 122 } 123 124 set(newCenter) { 125 origin.x = newCenter.x - size.width / 2 126 origin.y = newCenter.y - size.height / 2 127 } 128 } 129 130 //使用mutating关键字来修饰,可以修改属性的值 131 mutating func calCenter() { 132 self.center = Point(x: self.origin.x + self.size.width / 2, y:self.origin.y + self.size.height / 2) 133 } 134 135 init(x: Double, y: Double, width: Double, height: Double) { 136 self.origin.x = x 137 self.origin.y = y 138 self.size.width = width 139 self.size.height = height 140 } 141 } 142 143 var rect = Rect(x: 10, y: 20, width: 200, height: 150) 144 145 var rect1 = Rect(x: 100, y: 100, width: 100, height: 100) 146 rect1.center 147 rect1.center = Point(x: 200, y: 200) 148 149 150 //类型属性 151 //在结构体中使用static来声明类方法和类属性 152 //在类中使用class来声明类方法 153 154 struct Stu { 155 static var xiaoming = "小明" 156 157 static func sayHi() { 158 println("Hi") 159 } 160 } 161 162 Stu.xiaoming 163 var stu = Stu() 164 Stu.sayHi() 165 166 class Stu1 { 167 var xiaoming = "小明" 168 class func sayHi() { 169 println("Hi") 170 } 171 } 172 173 Stu1.sayHi() 174 175 176 //结构体是值类型 177 //值类型被赋予给一个变量,常量或者本身被传递给一个函数的时候,实际上操作的是其的拷贝 178 179 length 180 let length1 = length 181 182 length.start = 20 183 length.end = 30 184 185 length1 186 187 188 //类是引用类型 189 //引用类型在被赋予到一个变量,常量或者被传递到一个函数时,操作的是已存在的实例本身而不是其拷贝 190 191 192 car 193 let car1 = car 194 car.name = "玛莎拉蒂??" 195 car1 196 197 198 //类跟结构体的选择 199 //按照通用的准则:当符合以下七种一条或者多条时,请考虑使用结构体 200 //1.结构体的使用目的:用来封装尽量少的简单数据 201 //2.结构体拷贝的时候,属于值拷贝 202 //3.任何在结构体中存储的值类型属性都会被拷贝而不是引用 203 //4.结构体无需去继承另一个已存在的类型的属性或者行为 204 205 206 //练习 207 struct Student { 208 var name: String 209 var age: Int 210 211 init(name: String, age: Int) { 212 self.name = name 213 self.age = age 214 } 215 216 func printStudentInfo() { 217 println("name:\(name), age:\(age)") 218 } 219 mutating func changeName (newName: String) { 220 self.name = newName 221 } 222 223 static func printStudentSorry() { 224 println("I‘m sorry!") 225 } 226 } 227 228 var stu3 = Student(name: "别人家的孩子", age: 18) 229 stu3.changeName("乔明") 230 231 stu3.printStudentInfo() 232 233 Student.printStudentSorry() 234 235 236 class Teacher { 237 var name: String 238 var age: Int 239 240 init(name: String, age: Int) { 241 self.name = name 242 self.age = age 243 } 244 245 func printTeachInfo() { 246 println("name:\(name), age:\(age)") 247 } 248 249 class func printTeachSorry() { 250 println("I‘m sorry!") 251 } 252 } 253 254 var teacher = Teacher(name: "你妹", age: 20) 255 teacher.printTeachInfo() 256 Teacher.printTeachSorry() 257 258 259 260 //======================================= 261 class Person { 262 var identify = "Person" 263 var name: String? 264 var sex: String? 265 let age: Int? 266 //定义变量/长粮食,可以不对其初始化,但是一定要在构造方法中进行初始化 267 init(name: String, sex: String) { 268 self.name = name 269 self.sex = sex 270 self.age = 25 271 } 272 273 deinit { 274 275 } 276 277 func personInfo() { 278 println("name=\(name!), sex=\(sex!), age=\(age!)") 279 } 280 281 class func PersonInfo() -> String { 282 return "他是个人" 283 } 284 285 class var xiang: String { 286 get { 287 return "飞翔??" 288 } 289 set(newXiang) { 290 self.xiang = newXiang 291 } 292 } 293 294 } 295 296 var person = Person(name: "乔明", sex: "女") 297 person.personInfo() 298 299 Person.xiang 300 301 var personA = Person(name: "豆豆", sex: "男") 302 var personB = Person(name: "快递妹", sex: "女") 303 personA.personInfo() 304 personB.personInfo() 305 306 307 class Coder: Person { 308 override init(name: String, sex: String) { 309 super.init(name: name, sex: sex) 310 self.identify = "Coder" 311 } 312 313 override func personInfo() { 314 println("我是一个悲伤地Coder" + "??") 315 } 316 } 317 318 var coder: Person = Coder(name: "程序猿??", sex: "不明") 319 coder.personInfo() 320 321 322 if coder is Coder { 323 println("Coder") 324 } else { 325 println("Person") 326 } 327 328 329 //协议 330 protocol aProtocol { 331 func funcOne() 332 } 333 334 //结构体遵守协议 335 struct aStudent: aProtocol { 336 func funcOne() { 337 println("结构体,协议") 338 } 339 } 340 341 //类遵守协议 342 class aClass: aProtocol { 343 func funcOne() { 344 println("类,协议") 345 } 346 } 347 348 //类既有父类又遵循某种协议 349 class bClass: Person, aProtocol { 350 func funcOne() { 351 println("类,协议") 352 } 353 } 354 355 //声明一个协议,如果里面有optional,需要在protocol前面加@objc 356 @objc protocol bProtocol { 357 func funcA() 358 optional func funcB() 359 } 360 361 class cClass: bProtocol { 362 func funcA() { 363 println("") 364 } 365 }
标签:
原文地址:http://www.cnblogs.com/newBlash/p/4273616.html