本文概述
- 定义类层次结构
- 如何进行类型检查
Swift 4类型转换提供两个运算符:
- ‘ is’ :此运算符用于检查值的类型。
- ‘ as’ :此运算符用于将类型值转换为其他类型。
例:
class Subjects {var physics: Stringinit(physics: String) {self.physics = physics}}class Chemistry: Subjects {var equations: Stringinit(physics: String, equations: String) {self.equations = equationssuper.init(physics: physics)}}class Maths: Subjects {var formulae: Stringinit(physics: String, formulae: String) {self.formulae = formulaesuper.init(physics: physics)}}let sa = [ Chemistry(physics: "Mechanics", equations: "Hertz"), Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")]let samplechem = Chemistry(physics: "Mechanics", equations: "Hertz")print("Instance physics is: \(samplechem.physics)")print("Instance equation is: \(samplechem.equations)")let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")print("Instance physics is: \(samplemaths.physics)")print("Instance formulae is: \(samplemaths.formulae)")
输出
Instance physics is: MechanicsInstance equation is: HertzInstance physics is: Fluid DynamicsInstance formulae is: Giga Hertz
如何进行类型检查【Swift 4类型转换介绍和用法示例】” is” 运算符用于进行类型检查。它检查实例是否属于特定的子类类型, 如果实例属于, 则返回” true” , 否则返回” false” 。
推荐阅读
- Swift中的计时器解释和用法示例
- Swift 4初始化介绍和用法示例
- Swift反初始化介绍和用法
- Swift 4下标用法示例
- Swift中的继承介绍和使用示例
- Swift 4方法介绍和用法详细示例
- 笔记本声音小怎样办,本文教您笔记本电脑声音小怎样办
- win10传奇花屏,本文教您win10玩传奇出现花屏怎样办
- IE修好专家_本文教您IE浏览器修好专家