如何使用 repl 创建对象中定义的案例 class 的实例
how to use repl to create instance of a case class defined in object
在 Scala repl 中加载方法后,我在创建实例 class 时遇到问题。
这是代码。
object Game {
def main(args: Array[String]) {
val player1 = new Player("monu", 344)
val player2 = new Player("pankaj", 78)
declareWinner(player1, player2)
}
def printWinner(p: Player): Unit =
println(p.name + " is the winner!")
def declareWinner(p1: Player, p2: Player): Unit =
if (p1.score > p2.score) printWinner(p1)
else printWinner(p2)
case class Player(name: String, score: Int)
}
输出:
scala> :paste Game.scala
Pasting file Game.scala...
defined object Game
scala> val bob = Player("Bob",8 )
<console>:7: error: not found: value Player
val bob = Player("Bob",8 )
但如果我删除对象定义并只在代码中保留方法,则没有问题:
def main(args: Array[String]) {
val player1 = new Player("monu", 344)
val player2 = new Player("pankaj", 78)
declareWinner(player1, player2)
}
def printWinner(p: Player): Unit =
println(p.name + " is the winner!")
def declareWinner(p1: Player, p2: Player): Unit =
if (p1.score > p2.score) printWinner(p1)
else printWinner(p2)
case class Player(name: String, score: Int)
输出:
scala> :paste Game.scala
Pasting file Game.scala...
main: (args: Array[String])Unit
printWinner: (p: Player)Unit
declareWinner: (p1: Player, p2: Player)Unit
defined class Player
scala> val bob = Player("Bob",8 )
bob: Player = Player(Bob,8)
如果有人能提出解决第一种情况问题的方法,那将非常有帮助。
在第一个示例中,您需要使用 Game.Player
而不是 Player
,因为它是 Game
的内部 class。
解决此问题的第一种方法是使用 Game.Player
访问 Player
案例 class 作为:
val bob = Game.Player("Bob",8 )
.
第二种方法是先导入 class,使用 import Game._
或 import Game.Player
,然后像 val bob = Player("Bob",8 )
.[=16= 之前一样执行代码]
在 Scala repl 中加载方法后,我在创建实例 class 时遇到问题。
这是代码。
object Game {
def main(args: Array[String]) {
val player1 = new Player("monu", 344)
val player2 = new Player("pankaj", 78)
declareWinner(player1, player2)
}
def printWinner(p: Player): Unit =
println(p.name + " is the winner!")
def declareWinner(p1: Player, p2: Player): Unit =
if (p1.score > p2.score) printWinner(p1)
else printWinner(p2)
case class Player(name: String, score: Int)
}
输出:
scala> :paste Game.scala
Pasting file Game.scala...
defined object Game
scala> val bob = Player("Bob",8 )
<console>:7: error: not found: value Player
val bob = Player("Bob",8 )
但如果我删除对象定义并只在代码中保留方法,则没有问题:
def main(args: Array[String]) {
val player1 = new Player("monu", 344)
val player2 = new Player("pankaj", 78)
declareWinner(player1, player2)
}
def printWinner(p: Player): Unit =
println(p.name + " is the winner!")
def declareWinner(p1: Player, p2: Player): Unit =
if (p1.score > p2.score) printWinner(p1)
else printWinner(p2)
case class Player(name: String, score: Int)
输出:
scala> :paste Game.scala
Pasting file Game.scala...
main: (args: Array[String])Unit
printWinner: (p: Player)Unit
declareWinner: (p1: Player, p2: Player)Unit
defined class Player
scala> val bob = Player("Bob",8 )
bob: Player = Player(Bob,8)
如果有人能提出解决第一种情况问题的方法,那将非常有帮助。
在第一个示例中,您需要使用 Game.Player
而不是 Player
,因为它是 Game
的内部 class。
解决此问题的第一种方法是使用 Game.Player
访问 Player
案例 class 作为:
val bob = Game.Player("Bob",8 )
.
第二种方法是先导入 class,使用 import Game._
或 import Game.Player
,然后像 val bob = Player("Bob",8 )
.[=16= 之前一样执行代码]