问:它似乎在操场上工作。我是否还需要覆盖名字和姓氏 init ?
类.swift
class Person {
let firstName : String let lastName : String init ( firstName : String , lastName : String ) { self . firstName = firstName self 。lastName = lastName } func getFullName () -> String { return " \( firstName ) \( lastName ) " } } }
class Doctor : Person {
override func getFullName () -> String { self . firstName = "博士" return " \( firstName ) \( lastName ) " } }让someDoctor = Doctor ( firstName : "T" , lastName : "J" ) someDoctor 。获取全名()
答:挑战要求您以 [Dr. ][姓]。您当前拥有的是 [fullName][lastName]。我想指出的另一件事是这一行:
自我。firstName = "博士"
您在这里所做的是为一个常量分配一个与 constant 的含义相矛盾的值。我已经修复了您的代码,它应该可以与以下代码段一起使用:
class Person {
let firstName : String let lastName : String init ( firstName : String , lastName : String ) { self . firstName = firstName self 。lastName = lastName } func getFullName () -> String { return " \( firstName ) \( lastName ) " } }
class Doctor : Person {
override func getFullName () -> String { return "Dr. \( lastName ) " // 返回 "Dr. J" } } let someDoctor = Doctor ( firstName : "T" , lastName : "J" )某个医生。获取全名()