Skip to Main Content
iOS 编程基础:Swift、Xcode 和Cocoa 入门指南
book

iOS 编程基础:Swift、Xcode 和Cocoa 入门指南

by Matt Neuburg
January 2017
Intermediate to advanced content levelIntermediate to advanced
552 pages
9h 43m
Chinese
China Machine Press
Content preview from iOS 编程基础:Swift、Xcode 和Cocoa 入门指南
172
4
class Dog {
var name : String
required init(name:String) {
self.name = name
}
func havePuppy(name name:String) -> Self {
return self.dynamicType.init(name:name)
}
}
class NoisyDog : Dog {
}
下面是测试代码:
let d = Dog(name:"Fido")
let d2 = d.havePuppy(name:"Fido Junior")
let nd = NoisyDog(name:"Rover")
let nd2 = nd.havePuppy(name:"Rover Junior")
如你所想,
d2
是个
Dog
,不过
nd2
却是个类型为
NoisyDog
NoisyDog
讲了这么多可能有些乱,下面来总结一下:
.dynamicType
用在代码中,发送给实例:表示该实例的多态化(内部)类型,无论实例引用的类
型是什么均如此。静态
/
类成员可以通过实例的
dynamicType
进行访问。
.Type
用在声明中,发送给类型:多态类型而不是类型的实例。比如,在函数声明中,
Dog
表示需要一个
Dog
实例(或其子类的实例),
Dog.Type
则表示需要
Dog
类型本身(或
是其子类的类型)。
.self
用在代码中,发送给类型。比如,在需要
Dog.Type
时,你可以传递
Dog.self
(向
实例发送
.self ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

Aurélien Géron
Swift 人工智能实战:从基础理论到AI 驱动的应用程序开发

Swift 人工智能实战:从基础理论到AI 驱动的应用程序开发

Mars Geldard, Jonathon Manning, Paris Buttfield-Addison, Tim Nugent

Publisher Resources

ISBN: 9787111556350