[英]Trouble with Scala's class and object
I am having trouble with finding the issue in my Scala code. 我在Scala代码中找到问题时遇到问题。 Task below.
任务如下。
Input: val team1: Team = new Team(8,6)
val team2: Team = new Team(6,4)
Output for 'play' method: team1.score = 4
team2.score = 0
Output for 'Winner' method: team1
Task: 任务:
Create a Scala class named "Team" and a Scala object named "Referee". 创建名为“Team”的Scala类和名为“Referee”的Scala对象。
Team will have: 团队将:
•State values of type Int representing the strength of team's offense and defense with a constructor to set these values. •Int类型的状态值表示团队攻击和防御的强度,使用构造函数来设置这些值。 The parameters for the constructor should be offense then defense
构造函数的参数应该是进攻然后防御
•A third state variable named "score" of type Int •第三个状态变量,名为Int的类型“得分”
Referee will have: 裁判将:
•A method named "play" that takes two Team objects as parameters and does not return a value. •名为“play”的方法,它将两个Team对象作为参数,但不返回值。 This method will alter the state of each input Team by setting their scores equal to their offense minus the other Team's defense.
此方法将通过将其得分设置为等于其进攻减去其他球队的防守来改变每个输入球队的状态。 If a Team's offense is less than the other Team's defense their score should be 0 (no negative scores)
如果球队的进攻小于其他球队的防守,他们的得分应为0(无负分数)
•A method named "Winner" that takes two Teams as parameters and returns the Team with the higher score. •名为“Winner”的方法,它将两个团队作为参数并返回具有较高分数的团队。 If both Teams have the same score, return a new Team object with offense and defense both set to 0
如果两个团队具有相同的分数,则返回一个新的Team对象,其中攻击和防御都设置为0
class Team(var offense: Int, var defense: Int){
var score: Int = 0 //<-Updated Variable
}
object Referee{
def play(team1: Team, team2: Team) = {
team1.score = team1.offense - team2.defense
team2.score = team2.offense - team1.defense
if (team1.offense < team2.defense){
team1.score = 0
}
else if(team2.offense < team1.defense){
team2.score = 0
}
}
def Winner(team1: Team, team2: Team) = {
if (team1.score > team2.score){
team1
}
else if(team2.score > team1.score){
team2
}
else{
new Team(0, 0)
}
}
}
Assignments don't have return values. 分配没有返回值。 So you're creating a new
Team
, and assigning it to the variable p
, and then throwing it away. 因此,您要创建一个新的
Team
,并将其分配给变量p
,然后将其丢弃。
Remove the val p =
part. 删除
val p =
part。
The "task" that you set is inherently non-functional so it doesn't sit well with Scala. 您设置的“任务”本质上是不起作用的,因此它与Scala不相符。 Just for fun, here is a more functional approach to the problem which avoids any mutable values:
只是为了好玩,这是一个更实用的方法来解决问题,避免任何可变的值:
case class Team(offense: Int, defense: Int) {
def score(other: Team): Int = Math.max(0, offense - other.defense)
}
case class TeamScore(team: Team, score: Int) {
def beats(other: TeamScore): Boolean = score > other.score
}
case class Result(score1: TeamScore, score2: TeamScore) {
def winner: Option[Team] =
if (score1 beats score2) {
Some(score1.team)
} else if (score2 beats score1) {
Some(score2.team)
} else {
None
}
}
object Referee {
def play(team1: Team, team2: Team): Result =
Result(
TeamScore(team1, team1.score(team2)),
TeamScore(team2, team2.score(team1))
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.