Swift 4 - 无法调用Spaceship.init'没有参数

时间:2017-07-19 02:22:41

标签: ios swift

这是我的代码。我已经坚持了一段时间。我似乎无法弄明白。我跟随的指南要求我在Fighter子类中使用super.init(),但每次尝试时它都会给我一个错误。

Caused by: org.hibernate.exception.SQLGrammarException: 
Error calling CallableStatement.getMoreResults at 
org.hibernate.exception.internal.SQLStateConversionDelegate.convert
(SQLStateConversionDelegate.java:106) at 
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert
(StandardSQLExceptionConverter.java:42) at 
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert
(SqlExceptionHelper.java:111)
at org.hibernate.result.internal.OutputsImpl.convert
at org.hibernate.result.internal.OutputsImpl.<init>
(OutputsImpl.java:55)
at org.hibernate.procedure.internal.ProcedureOutputsImpl.<init>
(ProcedureOutputsImpl.java:32)
at org.hibernate.procedure.internal.ProcedureCallImpl.buildOutputs
(ProcedureCallImpl.java:453)
at org.hibernate.procedure.internal.ProcedureCallImpl.getOutputs
(ProcedureCallImpl.java:404)
    at org .hibernate.procedure.internal.ProcedureCallImpl.outputs
(ProcedureCallImpl.java:663)
    at org.hibernate.procedure.internal.ProcedureCallImpl.execute
(ProcedureCallImpl.java:646)
    ... 41 more
Caused by: java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'ADMSN_TABLE'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
class Spaceship {
    var name = String()
    var health = Int()
    var position = Int()
    init(name: String) {
        self.name = name
    }
    init(health: Int) {
        self.health = health
    }
    init(position: Int) {
        self.position = position
    }
    func moveLeft() {
        position -= 1
    }
    func moveRight() {
        position += 1
    }
    func wasHit() {
        health -= 5
    }
}

1 个答案:

答案 0 :(得分:0)

您没有初始化程序将类中的所有实例变量设置为传递的值。除非有特定的有效默认值,否则为每个值分别设置init是很奇怪的。我建议您阅读designated initializers并修改代码,以便在init(name:health:position:)中使用Spaceship初始化程序,在init(name:health:position:weapon:remainingFirePower:)中使用Fighter初始化程序调用super的实现并传递值

如果有任何值您永远不想成为空白字符串或零,则不应为它们提供默认值,因此需要在初始化程序中使用它们。

这相当于您的代码被修改为具有指定的初始值设定项,用于设置所有内容并具有默认值。

class Spaceship {
    var name : String
    var health : Int
    var position : Int
    init(name: String = "", health: Int = 0, position: Int = 0) {
        self.name = name
        self.health = health
        self.position = position
    }
    func moveLeft() {
        position -= 1
    }
    func moveRight() {
        position += 1
    }
    func wasHit() {
        health -= 5
    }
}

class Fighter: Spaceship {
    let weapon: String
    var remainingFirePower: Int
    init(name: String = "", health: Int = 0, position: Int = 0, weapon: String = "", remainingFirePower: Int = 0) {
        self.weapon = weapon
        self.remainingFirePower = remainingFirePower
        super.init(name: name, health: health, position: position)
    }

    func fire() {
        if remainingFirePower > 0 {
            remainingFirePower -= 1
        } else {
            print("You have no more fire power.")
        }
    }
}