Swift協議(Protocol)

協議是爲方法、屬性等定義一套規範,沒有具體的實現。html

協議可以被類、結構體等具體實現(或遵照)。dom

 

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片ide

  1. protocol SomeProtocol {  spa

  2.  // protocoldefinition goes here  .net

  3.  }  code

  4.  struct         SomeStructure:            FirstProtocol, AnotherProtocol {  orm

  5. // structure definition goes here}  htm

  6. class  SomeClass:    SomeSuperclass,     FirstProtocol, AnotherProtocol {  blog

  7.  // class definitiongoeshere  ip

  8.  }  


 

 

屬性

 

1. set 和 get 訪問器

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1.  protocol SomeProtocol {  

  2.  var mustBeSettable:Int { get set }  

  3. var doesNotNeedToBeSettable: Int { get }  

  4.  }  


 

2.靜態屬性

 

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. protocol AnotherProtocol {  

  2.  class var someTypeProperty: Int { get set }  

  3.  }  


 

3.只讀

 

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. protocol FullyNamed {  

  2. var fullName: String { get }  

  3.  }  


 

 實例:

 

[html] view plaincopy在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 plaincopy在CODE上查看代碼片派生到個人代碼片

  1.  protocol RandomNumberGenerator{  

  2. func random() -> Double  

  3. }  


 

2.定義靜態方法

 

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1.  protocolSomeProtocol {  

  2.  class func someTypeMethod()  

  3. }  


 

實例:

 

[html] view plaincopy在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 plaincopy在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

相關文章
相關標籤/搜索