gpt4 book ai didi

java - 如何在案例类中重用类参数?

转载 作者:行者123 更新时间:2023-12-02 05:28:52 24 4
gpt4 key购买 nike

我有一个这样的实体类:

case class Entity(id: Integer, name: String)

和类命令代码:

trait GeneralCommand {
val id: Option[Integer]
}

sealed abstract class EntityCommands(id: Option[Integer]) extends DomainCommand {
def this(id: Integer) = this(Option(id))
}

case class UpdateEntity(entity: Entity, id: Integer = entity.id) extends EntityCommands(id)

在 Intellij Idea 中,此代码不会突出显示。但是sbt cleancompile提示说:

not found: value entity
case class UpdateEntity(entity: Entity, id: Integer = entity.id) extends EntityCommands(id)
^

是否可以在scala中的案例类中重用参数?

最佳答案

据我所知你不能这样做(SO上的某个地方有一个相关的问题,我目前找不到),简单的解决方案是添加一个apply方法:

case class UpdateEntity(entity: Entity, id: Integer) extends EntityCommands(id)

object UpdateEntity {
def apply(ent: Entity) = new UpdateEntity(ent, ent.id)
}

关于java - 如何在案例类中重用类参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25741012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com