Create an Object
Write a People class first
////People.swift//Class////Created by youxianming on 15/3/18.//Copyright (c) 2015 youxianming. All rights reserved.//Import Foundationclasspeople {//Variable Valuevar name:string var age:int//Initialize Methodinit () {Name="youxianming" Age=Ten } //class Method classFunc MaxAge ()Int {return - } classFunc minage ()Int {return 0 } //Common MethodsFunc info (name:string, age:int)String {return "Age:\ (age) + name:\ (name)"} func subinfo ()-String {return "Age:\ (age) + name:\ (name)" }}
Then write a student class to inherit to the People class
// // student.swift// Class//// Created by youxianming On 15/3/18. // Copyright (c) 2015 youxianming. All rights reserved. // Import Foundation class student:people { var score:float override init () { 100.0 super.init () } }
Then the controller source code:
////Viewcontroller.swift//Class////Created by youxianming on 15/3/18.//Copyright (c) 2015 youxianming. All rights reserved.//Import UIKitclassViewcontroller:uiviewcontroller {//Initialize Variablesvar people:people var studuent:student=Student ()//Necessary InitializationRequired Init (coder Adecoder:nscoder) {//Call initialization MethodPeople =people () People.name="y.x.m."People.age= -super.init (Coder:adecoder)}Overridefunc viewdidload () {super.viewdidload () println (People.info ("youxianming", Age: -) ) println (People.subinfo ()) println (People.maxage ())}}
Some areas to note:
[Swift] Creates an object