//协议1-14
//1.协议
/*
协议可以用来定义方法、属性、下标的声明,协议可以被枚举、结构体、类遵守,多个协议之间欧诺个逗号隔开
协议中定义方法时不能有默认参数值,默认情况下,协议中定义的内容必须全部实现,也有办法办到只实现部分内容。
协议定义get、set,用var存储属性或get、set计算属性去实现
*/
protocol Drawable {
func draw()
var x: Int { get set } //协议中定义属性时必须用var关键字
var y: Int { get } //协议定义get用任何属性都可以实现
subscript(index: Int) -> Int { get set }
}
protocol Test1 { }
protocol Test2 { }
protocol Test3 { }
class TestClass: Test1, Test2, Test3 { }
class Person0: Drawable {
var x: Int = 0
let y: Int = 0 //实现协议时的属性权限要求不小于协议中定义的属性权限
func draw() {
print("Person0 draw")
}
subscript(index: Int) -> Int {
set {}
get { index }
}
}
class Person1: Drawable {
var x: Int {
get { 0 }
set {}
}
var y: Int { 0 }
func draw() {
print("Person1 draw")
}
subscript(index: Int) -> Int {
set {}
get { index }
}
}
//2.static、class
//为了保证通过(结构体枚举只能用static),协议中必须用static定义类型方法、类型属性、类型下标
protocol Drawable0 {
static func draw()
}
class Person2: Drawable0 {
class func draw() { print("Person2 draw") } //class 允许子类重写这个方法
}
class Person3: Drawable0 {
static func draw() { print("Person2 draw") } //static 不允许子类重写这个方法
}
//3.mutating
/*
只有将协议中的实例方法标记为mutating,才允许结构体、枚举的具体实现修改自身内存,类在实现方法时不用加mutating,枚举、结构体才需要加mutating
*/
protocol Drawable1 {
mutating func draw()
}
class Size: Drawable1 {
var width: Int = 0
func draw() {
width = 0
}
}
struct Point : Drawable1 {
var x: Int = 0
mutating func draw() {
x = 10
}
}
//4.init
protocol Drawable2 {
init(x: Int, y: Int) //协议中还可以定义初始化器init
}
class Point2 : Drawable2 {
required init(x: Int, y: Int) {} //非final类实现时必须加上required
}
final class Size2 : Drawable2 {
init(x: Int, y: Int) {}
}
//如果从协议实现的初始化器,刚好是重写了父类的指定初始化器,那么这个初始化必须同时加required、override
protocol livable {
init(age: Int)
}
class Person4 {
init(age: Int) {}
}
class Student: Person4, livable {
required override init(age: Int) {
super.init(age: age)
}
}
//5.init、init?、init!
/*
协议中定义的init?、init!,可以用init、init?、init!去实现
协议中定义的init,可以用init、init!去实现
*/
protocol livable0 {
init()
init?(age: Int)
init!(no: Int)
}
class Person5: livable0 {
required init(){}
// required init!() {}
required init?(age: Int) {}
// required init!(no: Int) {}
// required init(age: Int) {}
required init!(no: Int) {}
// required init?(no: Int) {}
// required init(no: Int) {}
}
//6.协议的继承:一个协议可以继承其他的协议
protocol Runnable {
func run()
}
protocol Livable1: Runnable {
func breath()
}
class Person: Livable1 {
func breath() {}
func run() {}
}
//7.协议的组合:可以包含1个类类型(最多1个)
protocol Livable6 {}
protocol Runnable6 {}
class Person6 {}
func fn0(obj: Person6) {} //接受Person6或者其子类的实例
func fn1(obj: Livable6) {} //接受遵守livable6协议的实例
func fn2(obj: Livable6 & Runnable6) {} //接受同时遵守livable6、Runnable6协议的实例
func fn3(obj: Person6 & Livable6 & Runnable6) {}
//接受同时遵守:Livable6、Runnable6协议、并且是Person或者其子类的实例
typealias RealPerson = Person6 & Livable6 & Runnable6
func fn4(obj: RealPerson) {}
//8.CaseIterabble
//让枚举遵守CaseIterabble协议,可以实现遍历枚举值
enum Season : CaseIterable {
case spring, summer, autumn, writer
}
let seasons = Season.allCases //////
print(seasons.count)
for season in seasons { //spring summer autumn winter
print(season)
}
//9.CustomStringConvertible
//遵守CustomStringConvertible协议可以自定义实例的打印字符串
class Person7 : CustomStringConvertible {
var age : Int
var name : String
init(age: Int, name: String) {
self.age = age
self.name = name
}
var description: String {
"age=\(age),name=\(name)"
}
}
var p = Person7(age: 10, name: "Jack")
print(p)
/*
遵守CustomStringConvertible、CustomDebugStringConvertible都可以自定义实例打印字符串
print用的是CustomStringConvertible协议的description
debugPrint、po调用的是CustomDebugStringConvertible协议的debugDescription
*/
class Tiger : CustomStringConvertible, CustomDebugStringConvertible {
var age = 0
var description: String { "tiger_\(age)" }
var debugDescription: String { "debug_tiger_\(age)" }
}
var tiger = Tiger()
print(tiger) //tiger_0
debugPrint(tiger) //debug_tiger_0
//10.Any、Anyobject
/*
swift提供了2种特殊的数据类型Any、AnyObject
Any可以代表任意类型(枚举、结构体、类,也包括函数类型)
AnyObjct可以代表任意类类型(在协议后面写上:AnyObject代表只有类能遵守这个协议)
*/
var stu: Any = 10
stu = "Jack"
stu = Student(age: 10)
//创建一个能存放任意类型的数组
//var data = Array<Any>()
var data = [Any]()
data.append(1)
data.append(3.14)
data.append(Student(age: 12))
data.append("Jack")
data.append({10})
print(data) //[1, 3.14, __lldb_expr_155.Student, "Jack", (Function)]
//11.is、as?、as!、as
//is用来判断是否是某种类型,as用来做强制类型转换
protocol Runnable8 { func run() }
class Person8 {}
class Student8: Person8,Runnable8 {
func run() {
print("Student run")
}
func study() {
print("Student study")
}
}
var stu8: Any = 10
print(stu8 is Int) //true
stu8 = "Jack"
print(stu8 is String)//true
stu = Student8()
print(stu8 is Person8) //false
print(stu8 is Student8)//false
print(stu8 is Runnable8)//false
(stu8 as? Student8)?.study() //没有调用study
stu8 = Student8()
(stu8 as? Student8)?.study() //Student study
(stu8 as! Student8).study() //Student study
(stu8 as? Runnable8)?.run() //Student run
var data0 = [Any]()
data0.append(Int("123") as Any)
var d = 10 as Double
print(d) //10.0
//12.X.self、X.Type、AnyClass
//X.self是一个元类型(metadata)的指针,metadata存放着类型相关信息,X.self属于X.Type类型
class Person9 {}
class Student9: Person9 {}
var perType: Person9.Type = Person9.self //self 相当于 class
var syuType: Student9.Type = Student9.self
perType = Student9.self
print(perType) //Student9
var anyType: AnyObject.Type = Person9.self
anyType = Student9.self
public typealias AnyClass = AnyObject.Type
var anyType2: AnyClass = Person9.self
anyType2 = Student9.self
print(anyType2)//Student9
var per = Person9()
var perType0 = type(of: per) //Person9
print(perType0)
print(Person9.self == type(of: per)) //true
//13.元类型的应用
class Animal { required init() {}}
class Cat: Animal {}
class Dog: Animal {}
class Pig: Animal {}
func create(_ clses: [Animal.Type]) -> [Animal] {
var arr = [Animal]()
for cls in clses {
arr.append(cls.init())
}
return arr
}
print(create([Cat.self, Dog.self, Pig.self]))
import Foundation
class Person10 {
var age: Int = 0
}
class Student10: Person10 {
var no: Int = 0
}
print(class_getInstanceSize(Student10.self)) //32
print(class_getSuperclass(Student10.self)!) //Person10
//swift有个隐藏的基类:Swift._SwiftObject
print(class_getSuperclass(Person10.self)!)//_TtCs12_SwiftObject
//14.Self一般用作返回值类型,限定返回值跟方法调用者必须是同意类型(也可以作为参数类型)instance
protocol Runing {
func test() -> Self
}
class Persons: Runing {
required init() {}
func test() -> Self {
type(of: self).init()
}
}
class Students : Persons {}
var pp = Persons()
print(pp.test())//__lldb_expr_190.Persons
var stuu = Students()
print(stuu.test())//__lldb_expr_190.Students
//Self代表当前类型
class Son {
var age = 1
static var count = 2
func run() {
print(self.age) //1
print(Self.count) //2 Self代表Son
}
}
网友评论