這是我的代碼:迅速沮喪的問題繼承
class Student {
var name: String?
init (name: String) {
self.name = name
}
}
class MasterStudent: Student {
var degree: String?
init(name: String, degree: String) {
self.degree = degree
super.init(name: name, degree: degree)
}
}
fun updateStudent(stu: Student) {
var count = 0
for st in studentArray {
if (st.id == stu.id) {
studentArray.removeAtIndex(count)
st as! MasterStudent //thread 1 signal :SIGABRT
studentArray.append(stu)
}
count += 1
}
}
如果我通過功能updateStudent
一個Student
對象,中投以MasterStudent
導致崩潰。我想將Student
對象變成MasterStudent
對象。
感謝
只要切斷那條線。這條線是沒有意義的(一個單獨的演員什麼也不做),並且你無論如何都沒有理由演員。 – matt
我認爲你濫用基本的OOP編程。 MasterStudent從Student繼承,所以你的**被強制downcast **可能導致崩潰。簡單地說,你不能總是從學生向MasterStudent低調。 – dfd