Swift协议(Protocol)

协议是为方法、属性等定义一套规范,没有具体的实现。

协议能够被类、结构体等具体实现(或遵守)。

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. protocol SomeProtocol {  
  2.  // protocoldefinition goes here  
  3.  }  
  4.  struct         SomeStructure:            FirstProtocol, AnotherProtocol {  
  5. // structure definition goes here}  
  6. class  SomeClass:    SomeSuperclass,     FirstProtocol, AnotherProtocol {  
  7.  // class definitiongoeshere  
  8.  }  

属性

1. set 和 get 访问器

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1.  protocol SomeProtocol {  
  2.  var mustBeSettable:Int { get set }  
  3. var doesNotNeedToBeSettable: Int { get }  
  4.  }  

2.静态属性

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. protocol AnotherProtocol {  
  2.  class var someTypeProperty: Int { get set }  
  3.  }  

3.只读

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. protocol FullyNamed {  
  2. var fullName: String { get }  
  3.  }  

 实例:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. struct Person: FullyNamed {  
  2.  varfullName: String  
  3.  }  
  4.  letjohnPerson(fullName: "John Appleseed")  
  5.  class Starship: FullyNamed {  
  6.  varprefix: String?  
  7.  varname: String  
  8. init(name: String, prefix: String? = nil) {  
  9.  self.name = name self.prefix = prefix  
  10. }  
  11.  varfullName: String {  
  12.  return (prefix ? prefix!+ " " :"")+ name  
  13.  }  
  14.  }  
  15.  varncc1701 = Starship(name: "Enterprise",prefix: "USS")  
  16.    

 方法

 1.定义方法

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1.  protocol RandomNumberGenerator{  
  2. func random() -> Double  
  3. }  

2.定义静态方法

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1.  protocolSomeProtocol {  
  2.  class func someTypeMethod()  
  3. }  

实例:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. protocol RandomNumberGenerator{  
  2.  funcrandom() -> Double  
  3.  }  
  4.  class                   LinearCongruentialGenerator:RandomNumberGenerator {  
  5. var lastRandom42.0let m = 139968.0  
  6. let a = 3877.0 let c = 29573.0  
  7. funcrandom() -> Double {  
  8.  lastRandom = ((lastRandom * a + c) %m)  
  9.  returnlastRandom / m  
  10.  }  
  11.  }  
  12.  let generatorLinearCongruentialGenerator()  
  13.  println("Here's       a        random         number:  
  14. \(generator.random())")  
  15.  //    prints    "Here's     a     random       number:0.37464991998171"  
  16.  println("And another one: \(generator.random())")  
  17.  //prints "And another one: 0.729023776863283"  

 

 把协议作为类型使用

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1.  protocol RandomNumberGenerator {  
  2.  func random() -> Double}  
  3.  class LinearCongruentialGenerator: RandomNumberGenerator {  
  4.  varlastRandom42.0 let m =139968.0  
  5. let a = 3877.0 letc = 29573.0  
  6. func random() -> Double {  
  7. lastRandom = ((lastRandom * a + c) %m)  
  8.  return lastRandom / m  
  9. }  
  10. }  
  11. class Dice {  
  12.  letsides: Int  
  13. let generator: RandomNumberGenerator  
  14.  init(sides: Int, generator: RandomNumberGenerator) {  
  15.  self.sides = sidesself.generator = generator  
  16. }  
  17. func roll() -> Int{  
  18. return Int(generator.random() * Double(sides)) + 1  
  19. }  
  20. }  
  21. vard6 = Dice(sides: 6, generator: LinearCongruentialGenerator())  
  22. for_ in 1...5 {  
  23. println("Randomdiceroll is \(d6.roll())")  
  24. }  

 

 

Swift交流讨论论坛论坛:http://www.cocoagame.net

欢迎加入Swift技术交流群:362298485

猜你喜欢

转载自guandongsheng.iteye.com/blog/2089459