Scala - 按运行时类型的构造函数参数选择构造函数

时间:2016-11-08 20:51:08

标签: scala

我有一个包含几个不同构造函数的类,当所有这些参数从同一个基类扩展时,它们的参数类型不同。

请参阅此处以获得简化示例:

Array
(
[channels] => Array
    (
        [0] => Array
            (
                [position] => 5
                [id] => 11
                [name] => AFK
            )

        [1] => Array
            (
                [position] => 1
                [id] => 22
                [name] => ARK
            )

        [2] => Array
            (
                [position] => 2
                [id] => 33
                [name] => ESO
            )

        [3] => Array
            (
                [position] => 4
                [id] => 44
                [name] => semi-afk
            )

        [4] => Array
            (
                [position] => 0
                [id] => 55
                [name] => SPACE
            )

        [5] => Array
            (
                [position] => 3
                [id] => 66
                [name] => Tanks & Ships
            )

    )

[instant_invite] => 
[id] => 123
[members] => Array
    (
        [0] => Array
            (
                [username] => Chartographer
                [status] => online
                [nick] => Chaz Rambone
                [avatar_url] => https://cdn.discordapp.com/embed/avatars/0.png
                [avatar] => 
                [discriminator] => 3270
                [id] => 124
            )

        [1] => Array
            (
                [username] => Chukers
                [status] => online
                [mute] => 
                [suppress] => 
                [deaf] => 
                [channel_id] => 789
                [game] => Array
                    (
                        [name] => The Elder Scrolls Online
                    )

                [avatar_url] => https://cdn.discordapp.com/embed/avatars/1.png
                [avatar] => 
                [self_deaf] => 
                [discriminator] => 9851
                [self_mute] => 
                [id] => 456
            )

    )

[name] => TEST

如何根据参数类型在运行时选择要使用的正确构造函数?

3 个答案:

答案 0 :(得分:1)

不完全确定你使用案例。虽然可以在案例中使用伴侣对象。

object MyNumber {
  def apply(typeId: Int): MyNumber = typeId match {
    case 1 => new MyNumber(new SpecificDataTypeOne)
    case 2 => new MyNumber(new SpecificDataTypeTwo)
  }
}

val mn = MyNumber(1)

答案 1 :(得分:1)

正如其他建议的那样,尝试将随播对象用作工厂(我仍然有错误,我已添加评论,但可能是它的scala版本依赖?)

object MyNumber {
  def apply(x:GeneralDataType) : MyNumber = x match {
      case SpecificDataTypeOne() => new MyNumber(1)
      case SpecificDataTypeTwo() => new MyNumber(2)
  }

  def getDataType(typeId: Int): GeneralDataType = typeId match {
      case 1 => new SpecificDataTypeOne
      case 2 => new SpecificDataTypeTwo
    }
    val x = getDataType(1)
    val mn = MyNumber(x)
}
case class MyNumber(myDataType: Int)


abstract case class GeneralDataType()
  case class SpecificDataTypeOne() extends GeneralDataType
  case class SpecificDataTypeTwo() extends GeneralDataType

答案 2 :(得分:0)

如果您只需要为一个GeneralDataType(或者几个)和一些构造函数执行此操作,模式匹配将满足您的需求:

x match {
  case y1: SpecificDataTypeOne => new MyNumber(y1)
  case y2: SpecificDataTypeTwo => new MyNumber(y2)
}

您可以使用反射制作更通用的解决方案,但只有在上述情况不够好的情况下才能使用它。