Swift协议(Protocol)

简介:

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

协议可以被类、结构体等详细实现(或遵守)。

 

protocol SomeProtocol {
 // protocoldefinition goes here
 }
 struct         SomeStructure:            FirstProtocol, AnotherProtocol {
// structure definition goes here}
class  SomeClass:    SomeSuperclass,     FirstProtocol, AnotherProtocol {
 // class definitiongoeshere
 }

 

 

属性

 

1. set 和 get 訪问器

 protocol SomeProtocol {
 var mustBeSettable:Int { get set }
var doesNotNeedToBeSettable: Int { get }
 }

 

2.静态属性

 

protocol AnotherProtocol {
 class var someTypeProperty: Int { get set }
 }

 

3.仅仅读

 

protocol FullyNamed {
var fullName: String { get }
 }

 

 实例:

 

struct Person: FullyNamed {
 varfullName: String
 }
 letjohn= Person(fullName: "John Appleseed")
 class Starship: FullyNamed {
 varprefix: String?
 varname: String
init(name: String, prefix: String? = nil) {
 self.name = name self.prefix = prefix
}
 varfullName: String {
 return (prefix ? prefix!+ " " :"")+ name
 }
 }
 varncc1701 = Starship(name: "Enterprise",prefix: "USS")
 

 方法

 1.定义方法

 protocol RandomNumberGenerator{
func random() -> Double
}

 

2.定义静态方法

 

 protocolSomeProtocol {
 class func someTypeMethod()
}

 

实例:

 

protocol RandomNumberGenerator{
 funcrandom() -> Double
 }
 class                   LinearCongruentialGenerator:RandomNumberGenerator {
var lastRandom= 42.0let m = 139968.0
let a = 3877.0 let c = 29573.0
funcrandom() -> Double {
 lastRandom = ((lastRandom * a + c) %m)
 returnlastRandom / m
 }
 }
 let generator= LinearCongruentialGenerator()
 println("Here's       a        random         number:
\(generator.random())")
 //    prints    "Here's     a     random       number:0.37464991998171"
 println("And another one: \(generator.random())")
 //prints "And another one: 0.729023776863283"

 把协议作为类型使用

 protocol RandomNumberGenerator {
 func random() -> Double}
 class LinearCongruentialGenerator: RandomNumberGenerator {
 varlastRandom= 42.0 let m =139968.0
let a = 3877.0 letc = 29573.0
func random() -> Double {
lastRandom = ((lastRandom * a + c) %m)
 return lastRandom / m
}
}
class Dice {
 letsides: Int
let generator: RandomNumberGenerator
 init(sides: Int, generator: RandomNumberGenerator) {
 self.sides = sidesself.generator = generator
}
func roll() -> Int{
return Int(generator.random() * Double(sides)) + 1
}
}
vard6 = Dice(sides: 6, generator: LinearCongruentialGenerator())
for_ in 1...5 {
println("Randomdiceroll is \(d6.roll())")
}




本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5178573.html,如需转载请自行联系原作者



相关文章
|
Swift iOS开发
Swift 语言: 什么是协议(Protocol)?如何实现和使用协议?
Swift 语言: 什么是协议(Protocol)?如何实现和使用协议?
446 2
|
算法 Swift C++
34 Swift为了协议 关联类型
Swift为了协议 关联类型
187 0
|
存储 Swift iOS开发
31 Swift 继续聊聊协议扩展
Swift 继续聊聊协议扩展
244 0
|
设计模式 Swift iOS开发
【Swift开发专栏】Swift中的协议与委托模式
【4月更文挑战第30天】Swift编程语言强调协议与委托模式。协议定义了类型需实现的方法和属性,如`SomeProtocol`示例。遵循协议的类、结构体或枚举需实现协议要求。协议可继承,也可作为类型使用。委托模式让对象间通信更灵活,通过协议实现,如`DataSourceDelegate`示例。实战案例展示了在`UITableView`和自定义下载器中使用委托模式。
351 0
|
存储 Swift 开发者
Swift 协议
Swift 协议
135 0
|
存储 安全 Swift
Swift高级特性:泛型与协议
【7月更文挑战第10天】Swift高级特性:泛型与协议增强代码复用与类型安全。泛型允许编写通用代码,如`swap`函数和泛型`Stack`结构体,支持类型约束如`Comparable`。协议定义行为蓝图,类型遵循协议需实现其要求。通过两者结合,构建高效灵活的代码结构。
|
Swift C++ Ruby
32 Swift面向协议编程初探
32 Swift面向协议编程初探
160 0
|
存储 前端开发 Swift
Swift实用小册20: Protocol协议的使用
在本章中,你将学会Protocol协议的使用方法。
427 0
Swift实用小册20: Protocol协议的使用
|
存储 安全 Java
Swift5.0 - day5-继承、初始化、可选链、协议(上)
Swift5.0 - day5-继承、初始化、可选链、协议(上)
257 0
Swift5.0 - day5-继承、初始化、可选链、协议(上)
|
Swift C++ 开发者
Swift5.0 - day12 - 面向协议编程
Swift5.0 - day12 - 面向协议编程
398 0

相关课程

更多