Im在加載Scala中的構造函數時遇到問題。每次我試圖傳遞一個值的過載CTOR我得到的錯誤在Scala中重載CTOR的問題
Example:
var client : Client = Client(*variable type List[String]()*);
Unspecified value parameter clientList.
我的目標是有使用2種不同的數據類型創建的對象。一個是NodeSeq和另一個列表。從來沒有。我是否正確加載CTOR或者是否有更高效的方法來實現我的目標?
package api
import xml._
case class Client(clientNode: NodeSeq, clientList: List[String]) {
def this(clientNode: NodeSeq) = this(clientNode, null)
def this(clientList: List[String]) = this(null, clientList)
var firstName: String
var lastName: String
var phone: String
var street: String
var city: String
var state: String
var zip: String
var products = List[String]()
var serviceOrders = List[String]()
if (clientList == null) {
firstName = (clientNode \\ "firstname").text
lastName = (clientNode \\ "lastname").text
phone = (clientNode \\ "phone").text
street = (clientNode \\ "street").text
city = (clientNode \\ "city").text
state = (clientNode \\ "state").text
zip = (clientNode \\ "zip").text
(clientNode \\ "products").foreach(i => products = i.text :: products)
(clientNode \\ "serviceOrders").foreach(i => serviceOrders = i.text :: serviceOrders)
} else {
firstName = clientList(0)
lastName = clientList(1)
phone = clientList(2)
street = clientList(3)
city = clientList(4)
state = clientList(5)
zip = clientList(6)
}
override def toString(): String = {
return "Name : " + firstName + " " + lastName +
"\nAddress : " +
"\n\t" + street +
"\n\t" + city + ", " + state + " " + zip
}
}
不要在Scala代碼中使用null。它只存在於與Java的互操作性。改用'Option'。 (對不起,每次我看到有人在使用'null'時都必須這樣說...)。 – Jesper 2012-04-03 19:59:00