私有Scala方法的可见性问题

杰森

我有以下Scala代码片段,它是包含更多类的较大源文件的一部分,其中包括私有字段,方法和公共方法:

class Grid {

  private val cells = Vector(
    Vector(new Cell, new Cell, new Cell),
    Vector(new Cell, new Cell, new Cell),
    Vector(new Cell, new Cell, new Cell)
  )



 private def tranpose(grid:Vector[Vector[Cell]]) : Vector[Vector[Cell]] = {
    val newgrid = Vector(
      Vector(grid(0)(0), grid(1)(0), grid(2)(0)),
      Vector(grid(0)(1), grid(1)(1), grid(2)(1)),
      Vector(grid(0)(2), grid(1)(2), grid(2)(2))
      )
    newgrid
     }

  // Determine winner or draw
    def wins(symbol:Char):Boolean  = {
       val fullvec = Vector(symbol, symbol, symbol)
       for(r<-cells)
          if(r.equals(fullvec))
            true

        // Transpose the grid into a new one and make the same check again
        val transpgrid = transpose(cells)
        for(r<-transpgrid)
           if(r.equals(fullvec))
             true

        // Now check diagonals
        val maindiag = Vector(cells(0)(0), cells(1)(1), cells(2)(2))
        val seconddiag = Vector(cells(0)(2), cells(1)(1), cells(2)(0))
        if(maindiag.equals(fullvec) || seconddiag.equals(fullvec)) 
           true
         false
    }

在代码的行val transpgrid = transpose(cells)wins的方法,scala给我的以下消息:

jasonfil@hp ~/AtomicScala/examples $ scala TicTacToe.scala 
TicTacToe.scala:69: error: not found: value transpose
    val transpgrid = transpose(cells)

我尝试将关键字添加this到之前transpose,但运气不佳。我是该语言的新手,并且认为我在通话时犯了某种错误。

//编辑:此后,我已将此帖子标记为主持人批准和删除,因为很明显,在创建最小示例(明显的错字)时,我没有给予足够的重视。但是,自那以后,我意识到我的这段代码的另一处错误是,在代码的区域中放任地不使用“ return”关键字,这些关键字显然不是其各自方法的最后一行。昨天这让我非常痛心,但我还是从心痛中吸取了教训。

迈克·库里(Mike Curry)

您有拼写错误-private def tranpose应该是private def transpose

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章