5.1创建类,类的属性,类的方法
//创建类class Door { //类的属性 var opened : Bool = false var locked : Bool = false let width : Int = 32 let height : Int = 72 let weight : Int = 10 var color : String = "Red" //类的方法 func open() -> String { opened = true return "C-r-r-e-e-a-k-k-k... the door is open!" } func close() -> String { opened = false return "C-r-r-e-e-a-k-k-k... the door is closed!" } func lock() -> String { locked = true return "C-r-r-e-e-a-k-k-k... the door is locked!" } func unlock() -> String { locked = false return "C-r-r-e-e-a-k-k-k... the door is unlocked!" }}
5.2创建对象,调用属性,方法,重新赋值
//创建类的实例对象let frontDoor = Door()//调用方法,属性let tempcolor = frontDoor .color//RedfrontDoor .close()//"C-r-r-e-e-a-k-k-k... the door is closed!"frontDoor .open()//"C-r-r-e-e-a-k-k-k... the door is open!"frontDoor.color = "Orange"//属性为变量才能重新赋值print("\(newFrontDoor.color)")//Orange
5.3初始化
class NewDoor { var opened : Bool = false var locked : Bool = false let width : Int let height : Int let weight : Int var color : String //初始化 init(width : Int = 32, height : Int = 72 , weight : Int = 10, color : String = "Red") { self.width = width self.height = height self.weight = weight self.color = color }}//不赋值 默认参数let newFrontDoor = NewDoor()newFrontDoor.width//32newFrontDoor.height//72newFrontDoor.weight//10newFrontDoor.color //Red//新对象重新赋值let newBackDoor = NewDoor(width : 36,height: 80,weight : 20,color: "Green")newBackDoor.width//36newBackDoor.color//Green
便利初始化方法
**关键字convenience打头 表示便利初始化方法**class Tractor { let horsePower : Int let color : String init(horsePower :Int,color : String) { self.horsePower = horsePower self.color = color } convenience init(horsePower : Int) { self.init(horsePower:horsePower,color:"Green") } convenience init() { self.init(horsePower:42,color:"Orange") }}let myBigTracter = Tractor()//42 Orangelet myBiggerTracter = Tractor(horsePower:71)//71,Greenlet myYardTaceter = Tractor(horsePower:16,color:"Red")//16,Red
5.4继承
基类
class Portal { var opened : Bool = false var locked : Bool = false let width : Int let height : Int let weight : Int let name : String var color : String //初始化 init(name : String ,width : Int = 32, height : Int = 72 , weight : Int = 10, color : String = "Red") { self.width = width self.height = height self.weight = weight self.color = color self.name = name } func open() -> String { if (opened == false){ opened = true return "C-r-r-e-e-a-k-k-k... the \(name) is open!" } else{ return "The \(name) is already open!" } } func close() -> String { if (opened == true){ opened = false return "C-r-r-e-e-a-k-k-k... the \(name) is closed!" }else{ return "The \(name) is already closed" } } func lock() -> String { if (opened == false){ locked = true return "C-r-r-e-e-a-k-k-k... the \(name) is locked!" }else{ return "You cannot lock an open \(name)" } } func unlock() -> String { if (opened == false){ locked = false return "C-r-r-e-e-a-k-k-k... the \(name) is unlocked!" }else{ return "You cannot unlock an open \(name)" } } }
子类
//子类class NiceDoor : Portal{ init(width : Int = 32 , height : Int = 72,weight : Int = 10,color : String = "Red") { super.init(name: "NiceDoor",width: width,height : height,weight :weight,color : color) }}class NiceWindow : Portal{ init(width : Int = 48 ,height : Int = 48,weight : Int = 5,color : String = "Blue") { super.init(name : "BadWindow",width : width,height : height , weight :weight,color : color) }}let sunRoomDoor = NiceDoor()sunRoomDoor.close()//The NiceDoor is already closed"sunRoomDoor.open()//"C-r-r-e-e-a-k-k-k... the NiceDoor is open!"sunRoomDoor.lock()//"You cannot lock an open NiceDoor"sunRoomDoor.unlock()//"You cannot unlock an open NiceDoor"sunRoomDoor.locked//falsesunRoomDoor.opened//truesunRoomDoor.width//32sunRoomDoor.height//72sunRoomDoor.weight//10let bayWindow = NiceWindow()bayWindow.close()//"The BadWindow is already closed"bayWindow.open()//"C-r-r-e-e-a-k-k-k... the BadWindow is open!"bayWindow.lock()//"You cannot lock an open BadWindow"bayWindow.unlock()//"You cannot unlock an open BadWindow"bayWindow.locked//falsebayWindow.opened//truebayWindow.width//48bayWindow.height//48bayWindow.weight//5
重写密码锁功能子类 override 关键字用于在子类创建同名方法
class CombinationDoor : NiceDoor{ var combinationCode : String?//可选类型 变量可为nil override func lock() -> String { return "This Method is not valid for a combination door!" } override func unlock() -> String { return "This Method is not valid for a combination door!" } func lock(combinationCode : String) -> String { if (opened == false){ if (locked == true){ return "The \(name) is already locked!" } self.combinationCode = combinationCode locked = true return "C-l-i-c-c-c-k-k...the \(name) is locked!" }else{ return "You cannot lock an open \(name)!" } } func unlock(combinationCode : String) -> String { if (opened == false){ if(locked == false){ return "The \(name) is already unlocked!" }else{ if (self.combinationCode != combinationCode){ return "Wrong code ... the \(name) is still locked!" } } locked = false return "C-l-i-c-c-c-k-k...the \(name) is unlocked!" }else{ return "You cannot unlock an open \(name)!" } }}let securityDoor = CombinationDoor()securityDoor.widthsecurityDoor.heightsecurityDoor.weightsecurityDoor.colorsecurityDoor.combinationCode//nilsecurityDoor.unlock()//"This Method is not valid for a combination door!"securityDoor.lock()//"This Method is not valid for a combination door!"securityDoor.unlock(combinationCode:"6809")//"The NiceDoor is already unlocked!"securityDoor.locked//falsesecurityDoor.lock(combinationCode: "6809")//"C-l-i-c-c-c-k-k...the NiceDoor is locked!"securityDoor.locked//truesecurityDoor.unlock(combinationCode: "1111")//"Wrong code ... the NiceDoor is still locked!"securityDoor.unlock(combinationCode: "6809")//"C-l-i-c-c-c-k-k...the NiceDoor is unlocked!"
便利初始化 关键字convenience打头 表示便利初始化方法
class Tractor { let horsePower : Int let color : String init(horsePower :Int,color : String) { self.horsePower = horsePower self.color = color } convenience init(horsePower : Int) { self.init(horsePower:horsePower,color:"Green") } convenience init() { self.init(horsePower:42,color:"Orange") }}let myBigTracter = Tractor()//42 Orangelet myBiggerTracter = Tractor(horsePower:71)//71,Greenlet myYardTaceter = Tractor(horsePower:16,color:"Red")//16,Red
5.5枚举
enum enumerationName{ //常量定义}
一般声明
enum FuelTypetemp{ case Gasoline,Gasoline1,Gasoline2 case Diesel case Biodiesel case Electric case NaturalGas}
映射
enum FuelType : String{ case Gasoline = "89 octane" case Diesel = "sulphur free" case Biodiesel = "vagetable oil" case Electric = "30 amps" case NaturalGas = "ciakbed methane"}
提取原始值 rawValue
let fuelCharacteristic = FuelType.Gasoline.rawValue
赋值变量
var engine : FuelType = FuelType.Gasoline//简化var easyEngine : FuelType = .NaturalGasvar vehicleName : String?switch engine {case FuelType.Gasoline: vehicleName = "Ford F-150"case .Diesel: vehicleName = "Ford F-250"case .NaturalGas: vehicleName = "Truck"default: vehicleName = nil}
5.6结构
结构是一种用于存储数据的组织构造,很多方面跟类相似,swift的结构可以包含方法,但不支持继承
struct structureName{ //变量和常量定义}print("Vehicle\(vehicleName) takes \(engine.rawValue)")enum transimissionType{ case Manual4Gear case Manual5gear case Automatic}struct Vehicle{ var fuel : FuelType var transimission : transimissionType var Str : String}var dieseAutomatic = Vehicle(fuel : .Diesel,transimission : .Automatic,Str : "car")var gasoline4Speed = Vehicle(fuel : .Gasoline,transimission : .Manual4Gear, Str : "elcCar")
5.7值类型和引用类型
值类型:将结构赋给变量或常量时,将创建其副本;将结构作为参数传递给函数时,情况亦如此。(结构)
struct Structure{ var copyVar : Int = 10}var struct1 = Structure() //创建struct1var struct2 = struct1struct2.copyVar = 20;print("\(struct1.copyVar)")//10print("\(struct2.copyVar)")//20
引用类型:不管将同一个对象赋给多少变量或常量,这些变量或常量都将指向同一个对象;其中每个变量或常量存储的都是指向这个对象的引用,而不是副本。(类)
class Class { var copyVar : Int = 10}var class1 = Class()var class2 = class1class2.copyVar = 20;print("\(class1.copyVar)")print("\(class2.copyVar)")struct Triangel{ var base : Double var height : Double func area() -> Double { return (0.5 * base) * height }}var TrriangelBase = Triangel.init(base: 10, height: 20)print("\(TrriangelBase.area())")//100