使用類和結構組織代碼

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//Red
frontDoor .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//32
newFrontDoor.height//72
newFrontDoor.weight//10
newFrontDoor.color //Red
//新對象從新賦值
let newBackDoor  = NewDoor(width : 36,height: 80,weight : 20,color: "Green")
newBackDoor.width//36
newBackDoor.color//Green

便利初始化方法swift

**關鍵字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 Orange
let myBiggerTracter = Tractor(horsePower:71)//71,Green
let myYardTaceter = Tractor(horsePower:16,color:"Red")//16,Red

5.4繼承

基類ide

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//false
sunRoomDoor.opened//true
sunRoomDoor.width//32
sunRoomDoor.height//72
sunRoomDoor.weight//10

let 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//false
bayWindow.opened//true
bayWindow.width//48
bayWindow.height//48
bayWindow.weight//5

重寫密碼鎖功能子類 override 關鍵字用於在子類建立同名方法code

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.width
securityDoor.height
securityDoor.weight
securityDoor.color
securityDoor.combinationCode//nil

securityDoor.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//false
securityDoor.lock(combinationCode: "6809")//"C-l-i-c-c-c-k-k...the NiceDoor is locked!"
securityDoor.locked//true
securityDoor.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 Orange
let myBiggerTracter = Tractor(horsePower:71)//71,Green
let 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
}

映射ci

enum FuelType : String{
	case Gasoline = "89 octane"
	case Diesel = "sulphur free"
	case Biodiesel = "vagetable oil"
	case Electric = "30 amps"
	case NaturalGas = "ciakbed methane"
}

提取原始值 rawValueget

let fuelCharacteristic = FuelType.Gasoline.rawValue

賦值變量it

var engine : FuelType = FuelType.Gasoline
//簡化
var easyEngine : FuelType = .NaturalGas

var 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的結構能夠包含方法,但不支持繼承io

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() //建立struct1
var struct2 = struct1
struct2.copyVar = 20;
print("\(struct1.copyVar)")//10
print("\(struct2.copyVar)")//20

引用類型:無論將同一個對象賦給多少變量或常量,這些變量或常量都將指向同一個對象;其中每一個變量或常量存儲的都是指向這個對象的引用,而不是副本。(類)

class Class {
	var copyVar : Int = 10
}

var class1 = Class()
var class2 = class1
class2.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
相關文章
相關標籤/搜索