6.使用协议和扩展进行规范化

##6.1协议闭包

/*
protocol ProtocolName{
	协议的成员
}
*/
protocol NewLockUnlockProtocol{
	//get 和 set 是特殊限定符,在协议中同时使用了set和get时,遵循协议的类或结构中,必须使用关键字var来声明相应的变量。若是只使用了get,则在遵循协议的类或结构中,声明变量时,可以使用关键字let,也可以使用关键字var
	var locked : Bool{get set}
	func lock() -> String
	func unlock() -> String

}

class Safe : NewLockUnlockProtocol{
	var locked : Bool = false
	func lock() -> String{
		locked = true
		return "Ding"
	}
	func unlock() -> String {
		locked = false
		return "Dong"
	}
}

class Gate : NewLockUnlockProtocol{
	var locked: Bool = false
	func lock() -> String {
		locked = true
		return "Clink"
	}
	func unlock() -> String {
		locked = false
		return "Clonk"
	}
}

let mySafe = Safe()
mySafe.lock()//Ding
mySafe.unlock()//Dong

let myGate = Gate()
myGate.lock()//Clink
myGate.unlock()//Clonk

#6.2遵循多个协议app

protocol AreaComputationProtocol{
	func computeArea() ->Double
}
protocol PerimeterComputationProtocol{
	func computePerimeter() ->Double
}
struct Rectangle : AreaComputationProtocol,PerimeterComputationProtocol{
	var width,height : Double
	
	func computeArea() -> Double {
		return width * height
	}

	func computePerimeter() -> Double {
		return width * 2 + height * 2
	}
}

var square = Rectangle(width : 3,height : 3)
var rectangle = Rectangle(width : 4,height : 6)

square.computeArea()//9
square.computePerimeter()//12

rectangle.computeArea()//24
rectangle.computePerimeter()//20

#6.3协议也可继承this

protocol TriangleProtocol : AreaComputationProtocol,PerimeterComputationProtocol{
	var a : Double {get set}
	var b : Double {get set}
	var c : Double {get set}
	var base : Double {get set}
	var height : Double {get set}
}

struct Triangle : TriangleProtocol{
	var a,b,c : Double
	var base , height: Double
	
	func computeArea() -> Double {
		return (base * height)/2
	}
	
	func computePerimeter() -> Double {
		return a + b + c
	}
}
var triangle1 = Triangle(a : 4,b : 5 ,c : 6,base : 12,height : 10)
triangle1.computeArea()//60
triangle1.computePerimeter()//15

#6.4委托code

protocol VendingMachineProtocol{
	var coinInserted : Bool {get set}
	func shouldVend() -> Bool

}

class Vendor : VendingMachineProtocol{
	var coinInserted: Bool = false
	func shouldVend() -> Bool {
		if coinInserted == true{
			coinInserted = false
			return true
		}
		return false
	}
}

class ColaMachine {
	var vendor : VendingMachineProtocol
	init(vendor : VendingMachineProtocol) {
		self.vendor = vendor
	}
	func insertCoin(){
		vendor.coinInserted = true
	}
	func pressColaButton() -> String{
		if vendor.shouldVend() == true{
			return "Here is Cola"
		}else{
			return "You must insert a coin!"
		}
	}
	func preesRootBeerButton() ->String{
		if vendor.shouldVend() == true{
		
			return "Here is a Root Beer"
		}else{
			return "You must insert a coin"
		}
	
	}
}

var vendingMachine = ColaMachine(vendor : Vendor())
vendingMachine.pressColaButton()//You must insert a coin!"
vendingMachine.insertCoin()
vendingMachine.preesRootBeerButton()//Here is a Root Beer
vendingMachine.insertCoin()
vendingMachine.pressColaButton()//Here is Cola"

#6.5扩展对象

extension ColaMachine{
	func pressDietColaButton()->String{
		if vendor.shouldVend() == true{
			return "Here is a Diet Cola!"
		}else{
		
			return "You must insert a coin!"
		}
	}
}
var newVendingMachine = ColaMachine(vendor : Vendor())
newVendingMachine.pressDietColaButton()//"You must insert a coin!"
newVendingMachine.insertCoin()
newVendingMachine.pressDietColaButton()//Here is a Diet Cola!"

##6.5.1扩展基本类型 ###6.5.1 MB和GB继承

extension Int{
	//计算属性
	var kb : Int { return self * 1_024}//千字节
	var mb : Int { return self * 1_024 * 1_024}//兆字节
	var gb : Int { return self * 1_024 * 1_024 * 1_024}//吉字节
}
var x : Int = 4.kb//4096
var y = 8.mb//8388608
var z = 2.gb//2147483648

###6.5.2温度ip

extension Double{
	var F : Double {return self}
	var C : Double {return (((self - 32.0) * 5.0) / 9.0)}
	var K : Double {return (((self - 32.0) / 1.8) + 273.15)}

}
var temperatureF = 80.4.F//80.40000000000001
var temperatureC = temperatureF.C//26.88888888888889
var temperatureK = temperatureF.K//300.0388888888889

###6.5.3给String类型添加方法get

extension String {
	func prependString (value : String) -> String{
	
		return value + self
	}
	
	func appendString(value : String) -> String{
	
		return self + value
	}

}

"x".prependString(value: "Prefix")//Prefixx
"y".appendString(value: "Prefix")//yPrefix

###6.5.4使用关键字mutating mutating方法适用于修改被扩展类的对象的实际值string

extension Int{
	mutating func triple(){
		self = self * 3
	}
}

var trip = 3
trip.triple()//9

extension String{
	mutating func decorate(){
		self = "*** " + self + " ***"
	}
}

var testString = "decorate this"
testString.decorate()//"*** decorate this ***"

###6.5.6扩展中使用闭包it

extension Int{
	func repeatMethod (work:()->()){
		for _ in 0..<self{
			work()
		}
	}
}
**work:() ->()不接受任何参数也不返回任何结果的代码块,参数名work**
**"_"表示不在意,这里不须要使用变量**

5.repeatMethod {
	print("repeat this string")
}
//repeat this string
//repeat this string
//repeat this string
//repeat this string
//repeat this string
相关文章
相关标签/搜索