美文网首页swift
typealias 类型别名

typealias 类型别名

作者: fordring2008 | 来源:发表于2017-02-04 09:21 被阅读102次

typealias 是用来为已经存在的类型重新定义名字的,通过命名,可以使代码变得更加清晰。

//给Int64起了一个IntMax的别名

public typealias IntMax =Int64

针对泛型

//声明一个泛型类

class Persion1 {}

//给这个泛型类起别名

typealias Work =Persion1

//使用这个泛型类,初始化

let arr11 =Work()

另一个场景

//协议1

protocol Cat { }

//协议2

protocol Dog { }

//定义一个别名,这个别名是协议Cat和Dog的公共遵守的协议

typealias Pet1 =Cat&Dog

//定义一个遵守了合体协议的类

class subPet1 :Pet1{ }

相关文章

网友评论

    本文标题:typealias 类型别名

    本文链接:https://www.haomeiwen.com/subject/kysjittx.html