使用 Vector 类型参数覆盖 java 方法

Overriding java method with Vector type parameter

我正在 scala 中实现来自 Jade 的 ContractNetInitiator,我需要覆盖此方法:

protected void handleAllResponses(java.util.Vector responses,java.util.Vector acceptances)

并这样实现:

override def handleAllResponses(responses: Vector[_], acceptances: Vector[_]): Unit = {
    var acceptProposal: ACLMessage = null
    var bestProposal = Integer.MAX_VALUE
    var bestProposer = ""

    // Find best proposal and reject all proposal
    val e = responses.elements()

    while (e.hasMoreElements) {
        val response: ACLMessage = e.nextElement().asInstanceOf[ACLMessage]

        if (response.getPerformative == ACLMessage.PROPOSE) {
            val reply = response.createReply()
            reply.setPerformative(ACLMessage.REJECT_PROPOSAL)
            acceptances.addElement(reply) // Can't add the reply : "Type mismatch, expected: _, actual: ACLMessage"

            if (response.getUserDefinedParameter("Value").toInt < bestProposal) {
                bestProposal = response.getUserDefinedParameter("Value").toInt
                bestProposer = response.getSender.getLocalName
                acceptProposal = reply
            }
        }
    }

    // Accept proposal
    if (acceptProposal != null) {
        acceptProposal.setPerformative(ACLMessage.ACCEPT_PROPOSAL)
    }
}

但是当我尝试添加对接受的回复时,我得到了 Type mismatch

我试图用"acceptances: Vector[ACLMessage]"和"acceptances: Vector[Any]"来改变"acceptances: Vector[_]",但是它不起作用,因为它与超级class不对应。

有没有办法向接受中添加元素?

你需要施放它:

 acceptances.asInstanceOf[Vector[ACLMessage]].addElement(reply)

通常这是要避免的事情,但在这种情况下,使用 raw types 并且只记录有效类型参数完全是图书馆的错。