//: Playground - noun: a place where people can play
import UIKit
//var str = "Hello, playground"
// 1.Swift中协议类似于别的语言里的接口,协议里只做方法的声明,包括方法名、返回值、参数等信息,而没有具体的方法实现
protocol Person {
// 读写属性
var name:String {get set}
// 只读属性
var age:Int{get}
// 类型方法
static func method1()
// 实例方法
func method2() -> Int
// 突变方法
mutating func method3()
}
// 2.协议可以继承另一个协议
protocol Animal {
func move()
}
protocol Bird:Animal {
func song()
}
class Chikend:Bird {
func song() {
print("小鸡叫")
}
func move() {
print("小鸡快跑")
}
}
// 3.如果某个类 继承了某个父类,又遵循了某个协议,那么冒号后面应该先写父类,再写协议
class CC:继承的父类, 协议1, 协议2 {
}
Swift - 协议(protocol)
最新推荐文章于 2024-06-19 17:47:53 发布
本文介绍了Swift中的协议概念,包括基本的协议定义、协议继承以及如何在类中实现这些协议。通过具体的代码示例展示了如何创建和使用协议,以及如何定义特定类型的协议如Bird,该协议继承自更通用的Animal协议。
819

被折叠的 条评论
为什么被折叠?



