original articles, welcome reprint. Reprint Please specify: Dongsheng's blog
Inheritance occurs between subclasses and parent classes, and is an inheritance relationship of a series of classes.
For example: Person is the root class in the class hierarchy, Student is a Person the direct subclass, Worker is a Person the direct subclass of the class.
The specific implementation code for this inheritance relationship class is as follows:
Class person { var name: string var age : int func description () -> String { return "\ (name) Age is: \" } convenience init () { self.init (name: "Tony") self.age = 18 } convenience init (name: string) { self.init (name: name, age: 18) } init (Name: string, age: int) { self.name = name self.age = age }} class student: person { var school: string init (name: string, age: int,school: string) { self.school = school super.init (name: name, age: age) }} class Worker: person { var factory: string init (name: string, age: int,factory: string) { self.factory = factory super.init (Name: name, age: age) }}
as an example, the following will introduce Swift type checking and conversion of classes, including is operators, as operator.
Use is operator
is operator to determine whether an instance is a type of a class. If the instance is a target type, the result returns true, otherwise false.
Let's look at an example:
Let student1 = student (name: "Tom", age: 18,school: "Tsinghua University")//Create a Student instance let student2 = student (name: "Ben", age: 28,school: "Peking University")//Create a Student instance let student3 = student (name: "Tony", age: 38,school: "University of Hong Kong")//Create Student instance let worker1 = worker (name: "Tom", age: 18,factory: "steel mill")// Create Worker instance Let worker2 = worker (name: "Ben", age: 20,factory: "power Plant")// Create worker Instances let people = [student1, student2,student3, worker1,worker2] //instances into the People array collection var studentcount = 0var workercount = 0 for item in people {//uses For in to iterate through the collection of people arrays if item is worker { ++workercount } else if item is student { ++ Studentcount }} print ("Number of workers: \ (Workercount) , number of students: \ (studentcount) . ")
we can judge in the loop body, ItemisWorker the expression is to determine whether the elements in the collection are Worker the instance of the class.
In a similar way, ItemisStudent the expression is to determine whether the elements in the collection are Student the instance of the class.
The output results are as follows:
number of workers: 2 , number of students: 3 .
Use as operator
in the introduction as operator, you should understand the type conversion of an object, and not all types can be converted to each other. Let's look at the following statement:
Let P1:person = Student (name: "Tom", Age:20,school: "Tsinghua University") Let P2:person = Worker (name: "Tom", Age:18,factory: "Steel mill") le T P3:person = person (name: "Tom", age:28)
this creates a3an instanceP1,P2,P3, the types are Person. P1is aStudentinstance,P2is aWorkerinstance,P3is a Personinstance. First, object type conversions must occur in the context of inheritance,P1and theP2are declared as Persontype, and the instance is created by the Personthe subtype is instantiated.
as the creator of this program, we knowP1is essentiallyStudentinstance, but on the surface it is Persontype, the compiler cannot inferP1is an instance of Person,Studentor isWorker. We can use isoperator to determine what kind of instance it is. It can then be used when converting asoperator converts it to a subclass type, which is the Persontype ofP1SwitchStudentsubclass type, this conversion is calledDown Transformation. This conversion is risky ifP1is not a target type, the conversion will fail. In order not to have an exception, we can useAs ?Converts it to an optional type of the target type, succeeds if it is converted, and returns if unsuccessfulNil.
P3 with the P1 and the P2 There's a big difference, because P3 is essentially Person instance and cannot be transformed downward.
Let's look at an example:
Let student1 = student (name: "Tom", age: 18,school: "Tsinghua University") let Student2 = student (name: "Ben", age: 28,school: "Peking University") let student3 = student (name: "Tony", age: 38,school: "The University of Hong Kong") let worker1 = Worker (name: "Tom", age: 18,factory: "steel Mill") Let worker2 = worker (name: " Ben ", age: 20,factory: " power Plant ") let people = [student1, student2,student3 , worker1,worker2] for item in people { if let student = item as? Student { print ("Student school:\ (Student.school)") } else if let worker = item as? Worker { print ("Worker factory:\ (worker.factory)") } }
Use for in Traverse people The collection of arrays. In the loop body, letstudent = Item as? Student statements use as ? operator to convert an element to a Student type. If the conversion succeeds, the element is assigned to the Student variable, otherwise nil is assigned to the Student variable. The conversion executes the code successfully.
The final output results are as follows:
Student School: Tsinghua University
Student School: Peking University
Student School: University of Hong Kong
Worker Factory: Steel Mills
Worker Factory: Power Plant
As ? The operator is used if the type conversion is not determined to be successful, if the successful conversion result is an optional type. If we can ensure that the conversion is successful, you can use as! The operator implicitly splits the package while converting.
The sample code is as follows:
... let people = [Student1, student2,student3, Worker1,worker2]...let stud1 = people[0] as? The first element of the Student//people array, print (STUD1), print (stud1!. Name) Let Stud2 = people[1] as! Student the second element of the//people array print (STUD2) print (stud2.name)
Output Result:
Optional (Student)
Student
code first element of the array, using as? operator is converted to student optional (Student) student optional type.
in your code people the second element of the array, using the as! the operator is converted to Student type, the conversion succeeds to Student type instances, rather than Student an optional type.
Welcome to follow Dongsheng Sina Weibo@tony_Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
650) this.width=650; "title=" 00.png "src=" http://s5.51cto.com/wyfs02/M01/7C/D6/ Wkiol1bzfhyqspa4aaas2mbeznc615.png "alt=" Wkiol1bzfhyqspa4aaas2mbeznc615.png "/>
More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php
This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://tonyguan.blog.51cto.com/701759/1747535
Learning Swift from scratch (Day48)--type checking and conversion