在 Scala 中使用类调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20192629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
method calling using classes in scala
提问by user2947615
I am trying to run this program but it is telling me that r is not found. How do you call the method since it is in a class? (I am assuming that the error is because of this)
我正在尝试运行此程序,但它告诉我未找到 r。因为它在一个类中,你如何调用该方法?(我假设错误是因为这个)
package pack
class Sud(val grid: List[List[Int]]) {
def r(r: Int): List[Int] =
//code
}
object Sud
{
def main(args: Array[String])
{
val puzzle=new Sudoku(List(
List(0, 0, 9, 0, 0, 7, 5, 0, 0),
//rest of Sudoku puzzle created by repeating the above statement
println(r(0)) //ERROR given here
}
}
回答by abronan
As said in the comments, in your code ris a class method. Hence you need to Instantiateyour Class Sudin order to call that method.
正如评论中所说,在您的代码中r是一个类方法。因此,您需要实例化您的类Sud才能调用该方法。
val inst: Sud = new Sud(puzzle)
println(inst.r(0))

