您的位置:首页 > 其它

第十三章:方法 第十四章:构造与析构 第十五章:类继承(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())")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: