简体   繁体   English

Scala特征实现

[英]Scala trait implementation

I have a case class: 我有一个案例课:

case class EvaluateAddress(addressFormat: String,
                             screeningAddressType: String,
                             value: Option[String]) {
}

This was working fine until I have a new use case where "value" parameter can be a class Object instead of String. 直到我有一个新的用例,其中“值”参数可以是类Object而不是String时,这种方法才能正常工作。

My initial implementation to handle this use case: 我最初处理此用例的实现:

case class EvaluateAddress(addressFormat: String,
                             screeningAddressType: String,
                             addressId: Option[String],
                             addressValue: Option[MailingAddress]) {

  @JsonProperty("value")
  def setAddressId(addressId: String): Unit = {
    val this.`addressId` = Option(addressId)
  }

  def this(addressFormat: String, screeningAddressType: String, addressId: String) = {
    this(addressFormat, screeningAddressType, Option(addressId), None)
  }

  def this(addressFormat: String, screeningAddressType: String, address: MailingAddress) = {
    this(addressFormat, screeningAddressType, None, Option(address))
  }
}

but I don't feel this is a good approach and it might create some problem in future. 但是我觉得这不是一个好方法,将来可能会引起一些问题。

What are the different ways I can accomplish the same? 我可以完成相同工作的方式有哪些?

Edit: Is there a way I can create a class containing three parameters: ** addressFormat, screeningAddressType, value** and handle both the use cases? 编辑:有没有一种方法可以创建包含三个参数的类:** addressFormat,screeningAddressType,value **并处理两个用例?

You can have a default value for fields in a case class. 您可以为案例类中的字段设置默认值。

So you can have the Optional fields default to None : 因此,您可以将Optional字段默认设置为None

case class EvaluateAddress(addressFormat: String,
                             screeningAddressType: String,
                             addressId: Option[String] = None,
                             addressValue: Option[MailingAddress] = None)

Then when you create a new instance of EvaluateAddress , you can choose to pass a value for either of addressId , or addressValue or both ..or nothing at all. 然后,当您创建一个新的EvaluateAddress实例时,可以选择为addressIdaddressValue或两者都传递一个值。

You do not need to provide auxilliary constructors here and neither the setter. 您无需在此处提供辅助构造函数,也无需提供设置器。 You could simply use the copy method provided by the case class. 您可以简单地使用case类提供的copy方法。

For example: 例如:

case class MailingAddress(email:String)
case class EvaluateAddress(addressFormat: String,
                             screeningAddressType: String,
                             addressId: Option[String],
                             addressValue: Option[MailingAddress])

scala> val y = EvaluateAddress("abc", "abc", None, None)
y: EvaluateAddress = EvaluateAddress(abc,abc,None,None)

scala> y.copy(addressId = Some("addressId"))
res0: EvaluateAddress = EvaluateAddress(abc,abc,Some(addressId),None)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM