如何在scala中调用对象中的私有函数?

问题描述 投票:0回答:1

这是为该类中的当前分数。

private var score: Int = 0

def initialiseTest2(): Game = {
  return new Game(List((3,3),(3,4),(3,5),(5,3),(5,4),(5,5)), List((4,4,(x: Int) => x+1), (6,3, if(score == 0){ (x: Int) => x+1 }else { (x: Int) => x+3 })), 3,2)
}

def initialiseTest2 是在对象中的部分,在那里有 if(score == 0) 那部分我需要调用私有的var score,我怎么做,当我只是输入 score时,它给出了一个错误说 not found: value score

scala function var
1个回答
1
投票

创建一个getter函数来返回分数的副本。然后,你可以从测试中访问当前的分数。

object Score {

 private var score: Int = 0

 def currentScore(): Int = score

}
© www.soinside.com 2019 - 2024. All rights reserved.