第十三章:方法 第十四章:构造与析构 第十五章:类继承(322)
2016-11-07 23:30
288 查看
源代码:
//程序目的:学习《从零开始学swift》 //第十三章:方法 第十四章:构造与析构 第十五章:类继承 //Create By ChenZhen import Foundation //class Account { // // var amount: Double = 10_000.00 //账户金额 // var owner: String = "Tony" //账户名 // //计算利息 // func interesWithRate(rate: Double) -> Double { // return rate * amount // } //} // //var myAccount = Account() ////调用实例方法 //print(myAccount.interesWithRate(rate: 0.088)) //类方法 //class Employee { // var no: Int = 0 // var name: String = "" // var job: String? // var salary: Double = 0 // var dept: Department? //} // //class Department { // var no: Int = 0 // var name: String = "" // var employees: [Employee] = [Employee]() // // func insertWithObject(anObject: AnyObject, atIndex index: Int) ->() { // let emp = anObject as! Employee // employees.insert(emp, at:index) // } //} //var dept = Department() // //var emp1 = Employee() //dept.insertWithObject(anObject: emp1, atIndex: 0) // //var emp2 = Employee() //dept.insertWithObject(anObject: emp2, atIndex: 0) // //var emp3 = Employee() //dept.insertWithObject(anObject: emp3, atIndex: 0) // //print(dept.employees.count) //结构体和枚举方法变异 //class Employee { // var no: Int = 0 // var name: String = "" // var job: String? // var salary: Double = 0 // var dept: Department? //} //struct Department { // var no: Int = 0 // var name: String = "" // var employees: [Employee] = [Employee]() // //在枚举和结构体中无法修改值类型变量属性。需在方法前加‘mutating’ // mutating func insertWithObject(anObject: AnyObject, atIndex index: Int) ->() { // let emp = anObject as! Employee // employees.insert(emp, at:index) // } //} //struct Department { // var no: Int = 0 // var name: String = "" // var employees = NSMutableArray() //[Employee] = [Employee]() // //在枚举和结构体中无法修改值类型变量属性。但可以修改引用类型属性。 // func insertWithObject(anObject: AnyObject, atIndex index: Int) ->() { // let emp = anObject as! Employee // employees.insert(emp, at:index) // } //} //var dept = Department() // //var emp1 = Employee() //dept.insertWithObject(anObject: emp1, atIndex: 0) // //var emp2 = Employee() //dept.insertWithObject(anObject: emp2, atIndex: 0) // //var emp3 = Employee() //dept.insertWithObject(anObject: emp3, atIndex: 0) // //print(dept.employees.count) //构造函数使用外部参数名 //class RectangleA { // var width: Double // var height: Double // // init(W width: Double, H height: Double) { // self.width = width // self.height = height // } //} // //var recta = RectangleA(W: 320, H: 480) //print("长方形A:\(recta.width) * \(recta.height)") //局部参数名可以直接作为外部参数名使用 //class RectangleA { // var width: Double // var height: Double // // init(width: Double, height: Double) { // self.width = width // self.height = height // } //} // //var recta = RectangleA(width: 320, height: 480) //print("长方形A:\(recta.width) * \(recta.height)") //以下写法只适用于结构体类型,结构体类型可以按照从上到下的顺序把属性名作为外部参数名 //struct RectangleA { // var width: Double // var height: Double // //} // //var recta = RectangleA(width: 320, height: 480) //print("长方形A:\(recta.width) * \(recta.height)") //构造函数重载 //class RectangleA { // var width: Double // var height: Double // // init(width: Double, height: Double) { // self.width = width // self.height = height // } // // init(W width: Double, H height: Double) { // self.width = width // self.height = height // } // // init(length: Double) { // self.width = length // self.height = length // } // // init() { // self.width = 640.0 // self.height = 940.0 // } //} // //var rectc1 = RectangleA(width: 320, height: 480) //print("长方形A:\(rectc1.width) * \(rectc1.height)") // //var rectc2 = RectangleA(W: 330, H: 480) //print("长方形A:\(rectc2.width) * \(rectc2.height)") // //var rectc3 = RectangleA(length:500.0) //print("长方形A:\(rectc3.width) * \(rectc3.height)") // //var rectc4 = RectangleA() //print("长方形A:\(rectc4.width) * \(rectc4.height)") //构造函数在结构体中的代理 //struct RectangleA { // var width: Double // var height: Double // // init(width: Double, height: Double) { // self.width = width // self.height = height // } // // init(W width: Double, H height: Double) { // self.width = width // self.height = height // } // // init(length: Double) { // self.init(W: length, H: length) // } // // init() { // self.init(width: 640.0, height: 940.0) // } //} // //var rectc1 = RectangleA(width: 320, height: 480) //print("长方形A:\(rectc1.width) * \(rectc1.height)") // //var rectc2 = RectangleA(W: 330, H: 480) //print("长方形A:\(rectc2.width) * \(rectc2.height)") // //var rectc3 = RectangleA(length:500.0) //print("长方形A:\(rectc3.width) * \(rectc3.height)") // //var rectc4 = RectangleA() //print("长方形A:\(rectc4.width) * \(rectc4.height)") //构造函数在类中的代理 //class RectangleA { // var width: Double // var height: Double // // init(width: Double, height: Double) { // self.width = width // self.height = height // } // // init(W width: Double, H height: Double) { // self.width = width // self.height = height // } // // convenience init(length: Double) { // self.init(W: length, H: length) // } // // convenience init() { // self.init(width: 640.0, height: 940.0) // } //} // //var rectc1 = RectangleA(width: 320, height: 480) //print("长方形A:\(rectc1.width) * \(rectc1.height)") // //var rectc2 = RectangleA(W: 330, H: 480) //print("长方形A:\(rectc2.width) * \(rectc2.height)") // //var rectc3 = RectangleA(length:500.0) //print("长方形A:\(rectc3.width) * \(rectc3.height)") // //var rectc4 = RectangleA() //print("长方形A:\(rectc4.width) * \(rectc4.height)") //析构函数,只适用于类 //class RectangleA { // var width: Double // var height: Double // // init(width: Double, height: Double) { // self.width = width // self.height = height // } // // init(W width: Double, H height: Double) { // self.width = width // self.height = height // } // // deinit { // print("调用析构函数...") // self.width = 0.0 // self.height = 0.0 // } //} // //var rectc1: RectangleA? = RectangleA(width: 320, height: 480) //print("长方形A:\(rectc1!.width) * \(rectc1!.height)") //rectc1 = nil // //var rectc2: RectangleA? = RectangleA(W: 330, H: 480) //print("长方形A:\(rectc2!.width) * \(rectc2!.height)") //rectc2 = nil //构造函数调用规则 class Person { var name: String var age: Int func description() -> String { return "\(name) 年龄是:\(age)" } 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) self.name = "陈振" self.age = 21 } convenience override init(name: String, age: Int) { self.init(name: name, age: age, school: "大连东软信息学院") } func toString() { print("Student:\(school) \(name) \(age)") } } let student = Student() print("学生:\(student.toString())")
相关文章推荐
- php构造方法中析构方法在继承中的表现
- php构造方法之析构方法在继承中的表现
- php构造方法中析构方法在继承中的表现
- 继承类的构造和析构方法
- 子类继承和调用父类的构造方法
- Java基础入门笔记-构造方法的继承
- C++ 继承的构造与析构
- 虚拟方法、动态方法、抽象方法、构造方法、方法重载、方法继承
- 041魔法方法:构造和析构
- 单一继承多次与多重继承的构造与析构
- 【转载】java继承中的构造方法(super)
- 【Java基础】继承中的代码块和构造方法的执行顺序探索
- 继承条件下的构造方法调用
- Scala学习第十二天 Scala中的继承:超类的构造、重写字段、重写方法代码实战
- PHP面向对象之构造方法与析构方法
- java继承中的构造方法
- 课堂例子验证—继承条件下的构造方法调用
- 第四十六课、继承中的构造与析构
- (教学思路 C#之类四)构造方法(静态构造方法、this、方法重载)、析构方法(修改新增)
- 面向对象基础 之-定义类,声明,属性,成员方法,传值,构造与析构方法