Original: http://coolketang.com/staticCoding/5a9925adac502e5d51cb4e8c.html
1. This lesson will explain to you, the application of the parent class when converting the type of the instance.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-dda25f36cf3f0b40.png)
2. First define a class called Creature.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-a489249bf5fbfed7.png)
3. Then add a name attribute to the class.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-99a30374e6522f4d.png)
4. Through the initialization method, set the value of the name property.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-cef50af20ea8bdef.png)
5. Next, create a class called Dog and inherit from the Creature class.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-0fd9e6c949f5345f.png)
6. Then add a host property to the class.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-e4063dbe70c4affe.png)
7. Add an initialization method.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-934b78034ce5d806.png)
8. Set the value of the host attribute in turn, and call the initialization method of the parent class to set the value of the name attribute.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-38045f39a7aee305.png)
9. Create an array of creature types as the parent class.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-844d17049d30ff27.png)
10. Add instance objects of the three subclasses to the array and set their name and owner properties respectively.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-0208536435f7b795.png)
11. Then through the loop statement, traverse the elements in the array.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-ecd9920749cfb2a7.png)
12. Cast the elements to be traversed by converting the identifiers, casting their types from the superclass to the subclass's type. In this way, the value of each attribute of the element can be obtained and output.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-83e29ed04ebfb581.png)
13. Use another loop statement and cast the array to the type of the subclass. Finally, click the display debug area icon at the bottom to observe all the logs output through the console.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-f88667abe5a20c4c.png)
14.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-30d13eeabe240b0c.png)
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-d167f51093238e19.png)
This article is organized from: "Swift4 Interactive Tutorial", the real
[Hands-on] teaching mode, get started with iOS development and Swift language at the fastest speed, free download from Apple Store App Store:
https://itunes.apple.com/cn/app/id1320746678
, or scan the QR code at the bottom of this page . Course supporting material download address:
data download