(作業)Swift類的屬性與方法
阿新 • • 發佈:2019-02-18
話不多說,先貼作業要求
因為Person類需要一個列舉型別的gender,所以先定義列舉
//性別的列舉
enum Gender: Int {
case male //男性
case female //女性
case unknow //未知
//過載>操作符,方便後面排序使用
static func >(lhs: Gender, rhs: Gender) -> Bool {
return lhs.rawValue < rhs.rawValue
}
}
然後定義Person類
//人類 class Person: CustomStringConvertible { var firstName: String //姓 var lastName: String //名 var age: Int //年齡 var gender: Gender //性別 var fullName: String { //全名 get { return firstName + lastName } } //構造方法 init(firstName: String, lastName: String, age: Int, gender: Gender) { self.firstName = firstName self.lastName = lastName self.age = age self.gender = gender } convenience init(firstName: String, age: Int, gender: Gender) { self.init(firstName: firstName, lastName: "", age: age, gender: gender) } convenience init(firstName: String) { self.init(firstName: firstName, age: 0, gender: Gender.unknow) } required convenience init() { self.init(firstName: "") } //過載== static func ==(lhs: Person, rhs: Person) -> Bool { return lhs.fullName == rhs.fullName && lhs.age == rhs.age && lhs.gender == rhs.gender } //過載!= static func !=(lhs: Person, rhs: Person) -> Bool { return !(lhs == rhs) } //實現CustomStringConvertible協議中的計算屬性,可以使用print直接輸出物件內容 var description: String { return "fullName: \(self.fullName), age: \(self.age), gender: \(self.gender)" } } var p1 = Person(firstName: "張") var p2 = Person(firstName: "張", age: 20, gender: .male) print(p1) //輸出fullName: 張, age: 0, gender: male print(p1 == p2) //輸出false print(p1 != p2) //輸出true
然後定義Teacher類
//教師類 class Teacher: Person { var title: String //標題 //構造方法 init(title: String, firstName: String, lastName: String, age: Int, gender: Gender) { self.title = title super.init(firstName: firstName, lastName: lastName, age: age, gender: gender) } init(title: String) { self.title = title super.init(firstName: "", lastName: "", age: 0, gender: .unknow) } convenience required init() { self.init(title: "") } //重寫父類的計算屬性 override var description: String { return "title: \(self.title), fullName: \(self.fullName), age: \(self.age), gender: \(self.gender)" } } var t1 = Teacher(title: "hello") print(t1) //輸出title: hello, fullName: , age: 0, gender: unknow
再定義Student類
//學生類 class Student: Person { var stuNo: Int //學號 //構造方法 init(stuNo: Int, firstName: String, lastName: String, age: Int, gender: Gender) { self.stuNo = stuNo super.init(firstName: firstName, lastName: lastName, age: age, gender: gender) } init(stuNo: Int) { self.stuNo = stuNo super.init(firstName: "", lastName: "", age: 0, gender: Gender.unknow) } required convenience init() { self.init(stuNo: 0) } //重寫父類的計算屬性 override var description: String { return "stuNo: \(self.stuNo), fullName: \(self.fullName), age: \(self.age), gender: \(self.gender)" } } var s1 = Student(stuNo: 2015110101) print(s1) //輸出stuNo: 2015110101, fullName: , age: 0, gender: unknow
最後按要求建立陣列和字典並進行相應的操作
//初始化一個空的Person陣列
var array = [Person]()
//生成5個Person物件
for i in 1...5 {
let temp = Person(firstName: "張", lastName: "\(i)", age: 20, gender: .male)
array.append(temp)
}
//生成3個Teacher物件
for i in 1...3 {
let temp = Teacher(title: "hello", firstName: "李", lastName: "\(i)", age: 21, gender: .female)
array.append(temp)
}
//生成4個Student物件
for i in 1..<5 {
let temp = Student(stuNo: 2015110100 + i, firstName: "王", lastName: "\(i)", age: 19, gender: .male)
array.append(temp)
}
//定義一個字典,用於統計每個類的物件個數
var dict = ["Person": 0, "Teacher": 0, "Student": 0]
for item in array {
if item is Teacher { //是否是Teacher類
dict["Teacher"]! += 1
} else if item is Student { //是否是Student
dict["Student"]! += 1
} else { //Person類
dict["Person"]! += 1
}
}
//輸出字典值
for (key, value) in dict {
print("\(key) has \(value) items")
}
//原始陣列
print("------------------------------")
for item in array {
print(item)
}
//根據age從大到小排序
print("------------------------------")
array.sort { return $0.age > $1.age}
for item in array {
print(item)
}
//根據全名從前往後排序
print("------------------------------")
array.sort { return $0.fullName < $1.fullName}
for item in array {
print(item)
}
//根據gender和age從大往小排序
print("------------------------------")
array.sort { return ($0.gender > $1.gender) && ($0.age > $1.age) }
for item in array {
print(item)
}
最後這段程式碼的輸出結果如下: