我希望能够从函数producer
初始化变量setProducer
。
object MyObj {
var producer = null
def setProducer(p: KafkaProducer[String,String]): Unit = {
producer = p
}
//...
}
然而它说Expression of type KafkaProducer[String,String] doesn't conform to expected type Null
。
那么,我该如何初始化producer
?
答案 0 :(得分:2)
您需要为变量提供类型,例如:
object MyObj {
var producer: KafkaProducer[String,String] = null
def setProducer(p: KafkaProducer[String,String]): Unit = {
producer = p
}
//...
}