我是Swift的新手,我想在构建此代码时遇到问题。 “表达太复杂,无法在合理的时间内解决;考虑将表达式分解为不同的子表达式”。
我认为我想在打印时管理属性的方法是错误的,但我找不到原因。
class Personne{
var name : String
var forename : String
init(name : String, forename:String) {
self.name = name
self.forename = forename
}
}
class UnilStudent : Personne{
var id : Int
var section : String
init(name :String, forename:String, id:Int, section:String) {
self.section = section
self.id = id
super.init(name:name, forename:forename)
}
func affiche() {
print("Unil student N°" + \(self.id) + " : " + \(self.name) + \(self.forename) + "\n" + \(self.section + "\n")}
}
class Assistants : UnilStudent{
var assistantship : String
init(name :String, forename:String, id:Int, section:String, assistantship : String){
self.assistantship = assistantship
super.init(name :name, forename:forename, id:id, section:section )
}
override func affiche() {
super.affiche()
print("Unil student N°" + \(self.id) + " : " + \(self.name) + " " + \(self.forename) + "\n from " + \(self.section) + "is a student assistant for" + \(self.assistantship) + "\n")}
func printid(){
print("L'id de " + \(self.name) + " " + \(self.forename) + " est " + \(self.id))
}
}
let Adrien = Assistants(name : "BOB", forename : "BOBBY", id : 12000000, section : "FDCA", assistantship : "ACT")
let Alice = UnilStudent(name: "Hapoka", forename : "Alice", id : 17000000, section : "HEC")
print(Adrien)
答案 0 :(得分:2)
问题与继承无关。
\(...)
用于字符串插值,只能在 in 字符串中使用,例如:
print("Unil student N°\(self.id) : \(self.name)\(self.forename)")
而不是
print("Unil student N°" + \(self.id) + " : " + \(self.name) + \(self.forename))