Scala重新加入Val级别(Setters / Getters)

时间:2015-12-09 18:47:12

标签: scala

我在Scala中创建了一个自定义类,如下所示:

class DQOpsStoreProfileStatusS extends Serializable{
  final val serialVersionUID = 1l
  private var _id:Int = _
  private var _runStatusId:Int = _
  private var _lakeHdfsPath:String = _

  def id = this._id
  def id_ = (_id:Int) = this._id = _id

  def runStatusId = this._runStatusId
  def runStatusId_ = (_runStatusId:Int) = this._runStatusId = _runStatusId

  def lakeHdfsPath = this._lakeHdfsPath
  def lakeHdfsPath_ = (_lakeHdfsPath:String) = this._lakeHdfsPath = _lakeHdfsPath
}

我在另一个看起来像这样的对象中引用了这个类:

 var colResult = new DQOpsStoreProfileStatusS
        colResult.fieldIndex = curIndex
        colResult.lakeHdfsPath = inputFileLocation

我的两位主持人抱怨“重新分配到Val”。我查看了一堆例子,无法弄清楚我做错了什么?

1 个答案:

答案 0 :(得分:2)

setter的方法名称应以_=结尾,不能包含空格:def id_=(_id:Int)等。而是定义没有名为id_runStatusId_和{{1}的参数的方法}}