Skip to content

Commit 46e9736

Browse files
committed
include subclass case, with and without superclass implementation
1 parent 91ee8e7 commit 46e9736

File tree

1 file changed

+59
-9
lines changed

1 file changed

+59
-9
lines changed

bk1ch04p196extensions/bk1ch04p196extensions/ViewController.swift

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,22 @@ class Rocket : Flier {
9999
print("zoooom")
100100
}
101101
}
102+
class AtlasRocket : Rocket {
103+
override func fly() {
104+
print("ZOOOOOM")
105+
}
106+
}
107+
class Daedalus : Flier {
108+
// nothing
109+
}
110+
class Icarus : Daedalus {
111+
func fly() {
112+
print("fall into the sea")
113+
}
114+
}
102115

103116
protocol Flier2 {
104-
func fly()
117+
func fly() // *
105118
}
106119
extension Flier2 {
107120
func fly() {
@@ -120,6 +133,20 @@ class Rocket2 : Flier2 {
120133
print("zoooom")
121134
}
122135
}
136+
class AtlasRocket2 : Rocket2 {
137+
override func fly() {
138+
print("ZOOOOOM")
139+
}
140+
}
141+
class Daedalus2 : Flier2 {
142+
// nothing
143+
}
144+
class Icarus2 : Daedalus2 {
145+
func fly() {
146+
print("fall into the sea")
147+
}
148+
}
149+
123150

124151
extension RawRepresentable {
125152
init?(_ what:RawValue) {
@@ -155,18 +182,30 @@ class ViewController: UIViewController {
155182

156183
let b = Bird()
157184
b.fly() // flap flap flap
185+
(b as Flier).fly() // flap flap flap
158186

159187
let i = Insect()
160188
i.fly() // whirr
161-
let f : Flier = Insect()
162-
f.fly() // flap flap flap
163-
let ok = f is Insect
164-
print(ok)
189+
(i as Flier).fly() // flap flap flap
165190

166191
let r = Rocket()
167192
r.fly() // zoooom
168193
(r as Flier).fly() // flap flap flap
169194

195+
let r2 = AtlasRocket()
196+
r2.fly() // ZOOOOOM
197+
(r2 as Rocket).fly() // ZOOOOOM
198+
(r2 as Flier).fly() // flap flap flap
199+
200+
let d = Daedalus()
201+
d.fly() // flap flap flap
202+
(d as Flier).fly() // flap flap flap
203+
204+
let d2 = Icarus()
205+
d2.fly() // fall into the sea
206+
(d2 as Daedalus).fly() // flap flap flap
207+
(d2 as Flier).fly() // flap flap flap
208+
170209
}
171210

172211
// but:
@@ -178,14 +217,25 @@ class ViewController: UIViewController {
178217

179218
let i = Insect2()
180219
i.fly() // whirr
181-
let f : Flier2 = Insect2()
182-
f.fly() // whirr (!!!)
183-
let ok = f is Insect2
184-
print(ok)
220+
(i as Flier2).fly() // whirr (!!!)
185221

186222
let r = Rocket2()
187223
r.fly() // zoooom
188224
(r as Flier2).fly() // zoooom (!!!)
225+
226+
let r2 = AtlasRocket2()
227+
r2.fly() // ZOOOOOM
228+
(r2 as Rocket2).fly() // ZOOOOOM
229+
(r2 as Flier2).fly() // ZOOOOOM (!!!)
230+
231+
let d = Daedalus2()
232+
d.fly() // flap flap flap
233+
(d as Flier2).fly() // flap flap flap
234+
235+
let d2 = Icarus2()
236+
d2.fly() // fall into the sea
237+
(d2 as Daedalus2).fly() // flap flap flap
238+
(d2 as Flier2).fly() // flap flap flap
189239

190240

191241
}

0 commit comments

Comments
 (0)