@@ -99,9 +99,22 @@ class Rocket : Flier {
99
99
print ( " zoooom " )
100
100
}
101
101
}
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
+ }
102
115
103
116
protocol Flier2 {
104
- func fly( )
117
+ func fly( ) // *
105
118
}
106
119
extension Flier2 {
107
120
func fly( ) {
@@ -120,6 +133,20 @@ class Rocket2 : Flier2 {
120
133
print ( " zoooom " )
121
134
}
122
135
}
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
+
123
150
124
151
extension RawRepresentable {
125
152
init ? ( _ what: RawValue ) {
@@ -155,18 +182,30 @@ class ViewController: UIViewController {
155
182
156
183
let b = Bird ( )
157
184
b. fly ( ) // flap flap flap
185
+ ( b as Flier ) . fly ( ) // flap flap flap
158
186
159
187
let i = Insect ( )
160
188
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
165
190
166
191
let r = Rocket ( )
167
192
r. fly ( ) // zoooom
168
193
( r as Flier ) . fly ( ) // flap flap flap
169
194
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
+
170
209
}
171
210
172
211
// but:
@@ -178,14 +217,25 @@ class ViewController: UIViewController {
178
217
179
218
let i = Insect2 ( )
180
219
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 (!!!)
185
221
186
222
let r = Rocket2 ( )
187
223
r. fly ( ) // zoooom
188
224
( 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
189
239
190
240
191
241
}
0 commit comments