'呼叫中的额外争论'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了'呼叫中的额外争论'相关的知识,希望对你有一定的参考价值。

一旦我从超级类中调用Enum数据类型,我的super.init就会出错。我在这做错了什么?

class Atleta : Pessoa 

    var beltRanking : Int = 1
    let previousGraduation : Date
    var nextGraduation : Date

    init(beltRanking: Int, previousGraduation: Date, nextGraduation: Date) 

        self.beltRanking = 1
        self.previousGraduation = previousGraduation
        self.nextGraduation = nextGraduation

        super.init(name: name, country: country, birthDay: birthDay, email: email, joiningDate: joiningDate, role: RolePessoa) 

            self.name = name
            self.country = country
            self.birthDay = birthDay
            self.email = email
            self.joiningDate = joiningDate
            let role = RolePessoa()

        

    

另一个文件/类的枚举声明:

enum RolePessoa 
    case Aluno
    case Funcionario
    case Instrutor
    case Visitante

这是Pessoa类:

class Pessoa 

    var bodyStats : BodyStats = BodyStats()
    let name : String
    let country : String
    let birthDay : Date
    let email : String
    let joiningDate : Date
    var role : RolePessoa

    //    var typeOfCar : CarType = .Coupe

    init(name: String, country: String, birthDay: Date, email: String, joiningDate: Date, role: RolePessoa) 

        self.name = name
        self.country = country
        self.birthDay = birthDay
        self.email = email
        self.joiningDate = joiningDate
        self.role = .Aluno

    


答案

问题说明

super.init(name: name, country: country, birthDay: birthDay, email: email, joiningDate: joiningDate, role: RolePessoa)类中调用Atleta方法后,代码中有括号。 Swift编译器认为它是作为最后一个参数传递的闭包。

这里的意图是调用super.init方法并在那里传递正确的参数。我们可以更改方法调用以实现目标:

super.init(name: name, country: country, birthDay: birthDay, email: email, joiningDate: joiningDate, role: .Aluno)

请注意,我们将.Aluno作为角色传递,因为它在Pessoa类初始化器中使用。

重构建议

您可能希望从初始化参数中删除role以简化代码或为其分配默认值,如下所示:

init(name: String, country: String, birthDay: Date, email: String, joiningDate: Date, role: RolePessoa = .Aluno) 

    self.name = name
    self.country = country
    self.birthDay = birthDay
    self.email = email
    self.joiningDate = joiningDate
    self.role = role

然后可以通过下一个方式更改超级初始化调用:

super.init(name: name, country: country, birthDay: birthDay, email: email, joiningDate: joiningDate)

请注意,在这种情况下,我们根本没有指定role,因为它默认分配了.Aluno。如果需要,我们还可以指定不同的角色:

super.init(name: name, country: country, birthDay: birthDay, email: email, joiningDate: joiningDate, role: .Funcionario)

有关初学者的好官方文档供初学者阅读:Initialization

以上是关于'呼叫中的额外争论'的主要内容,如果未能解决你的问题,请参考以下文章

开始/结束外观转换的不平衡呼叫

对于Web3.0是否需要区块链的争论

javascript JS的争论对象

sh Bash通过争论

Aop和Mono争论

理解Marx-11 私有财产的争论