Function Scala:从方法返回各种基类的构造函数

Function Scala:从方法返回各种基类的构造函数,function,scala,constructor,Function,Scala,Constructor,在Scala中,我试图从一个方法返回许多基类的构造函数,如下所示 Abstract class A case class B(foo : Int, bar : Int) extends A case class C(foo : Int) extends A object D { def foo(bar : Int) : _ => A = { bar match { case 1 => B //return c

在Scala中,我试图从一个方法返回许多基类的构造函数,如下所示

Abstract class A
case class B(foo : Int, bar : Int) extends A
case class C(foo : Int) extends A

object D
{
    def foo(bar : Int) : _ => A = 
    {
        bar match
        {
            case 1 => B //return constructor of B
            case 2 => C //return constructor of C
        }
    }
}
我希望能够像这样使用它:

D.foo11,2//使用参数1,2构造B的实例

D.foo21//使用参数1构造C的实例


但是,这现在不起作用

我认为您真正想要的是一个工厂:

trait A {   def foo }

object D {

  private class B extends A {
    def foo(val a: Int, val b:Int): = { println("foo_ab") }
  }

  private class C extends A {
    def foo(cal a: Int): = { println("foo_a") }
  }

  // your 'factory' method
  def apply(bar: Int):A = {
    if (bar == 1) return new B
    else return new C
  }

}
从现在起,您可以使用它:

val test = A(1) //Creates a B Object
val test2 = A(2) //creates a C Object
我希望这能帮助你

我扩展我的答案:


你也可以这样做:D.apply1.foo1,2假设foo是一个B类方法

我想你真正想要的是一个工厂:

trait A {   def foo }

object D {

  private class B extends A {
    def foo(val a: Int, val b:Int): = { println("foo_ab") }
  }

  private class C extends A {
    def foo(cal a: Int): = { println("foo_a") }
  }

  // your 'factory' method
  def apply(bar: Int):A = {
    if (bar == 1) return new B
    else return new C
  }

}
从现在起,您可以使用它:

val test = A(1) //Creates a B Object
val test2 = A(2) //creates a C Object
我希望这能帮助你

我扩展我的答案:

您也可以这样做:D.apply1.foo1,2假设foo是类B的方法,请尝试以下代码:

object D {
  def foo(bar: Int): Seq[Any] => A = {
    seq =>
      bar match {
        case 1 => B(seq(0).asInstanceOf[Int], seq(1).asInstanceOf[Int]) //return constructor of B
        case 2 => C(seq(0).asInstanceOf[Int]) //return constructor of C
      }
  }
}
object D {
  def foo(bar: Int)(seq: Seq[Any]): A =
    bar match {
      case 1 => B(seq(0).asInstanceOf[Int], seq(1).asInstanceOf[Int]) //return constructor of B
      case 2 => C(seq(0).asInstanceOf[Int]) //return constructor of C
    }
}
D.foo返回函数,该函数从seq参数创建实例

此代码的当前版本:

object D {
  def foo(bar: Int): Seq[Any] => A = {
    seq =>
      bar match {
        case 1 => B(seq(0).asInstanceOf[Int], seq(1).asInstanceOf[Int]) //return constructor of B
        case 2 => C(seq(0).asInstanceOf[Int]) //return constructor of C
      }
  }
}
object D {
  def foo(bar: Int)(seq: Seq[Any]): A =
    bar match {
      case 1 => B(seq(0).asInstanceOf[Int], seq(1).asInstanceOf[Int]) //return constructor of B
      case 2 => C(seq(0).asInstanceOf[Int]) //return constructor of C
    }
}
请尝试以下代码:

object D {
  def foo(bar: Int): Seq[Any] => A = {
    seq =>
      bar match {
        case 1 => B(seq(0).asInstanceOf[Int], seq(1).asInstanceOf[Int]) //return constructor of B
        case 2 => C(seq(0).asInstanceOf[Int]) //return constructor of C
      }
  }
}
object D {
  def foo(bar: Int)(seq: Seq[Any]): A =
    bar match {
      case 1 => B(seq(0).asInstanceOf[Int], seq(1).asInstanceOf[Int]) //return constructor of B
      case 2 => C(seq(0).asInstanceOf[Int]) //return constructor of C
    }
}
D.foo返回函数,该函数从seq参数创建实例

此代码的当前版本:

object D {
  def foo(bar: Int): Seq[Any] => A = {
    seq =>
      bar match {
        case 1 => B(seq(0).asInstanceOf[Int], seq(1).asInstanceOf[Int]) //return constructor of B
        case 2 => C(seq(0).asInstanceOf[Int]) //return constructor of C
      }
  }
}
object D {
  def foo(bar: Int)(seq: Seq[Any]): A =
    bar match {
      case 1 => B(seq(0).asInstanceOf[Int], seq(1).asInstanceOf[Int]) //return constructor of B
      case 2 => C(seq(0).asInstanceOf[Int]) //return constructor of C
    }
}
走走:

abstract class A
case class B(foo : Int, bar : Option[Int]) extends A
case class C(foo : Int, bar : Option[Int]) extends A

implicit def int2option(value : Int) : Option[Int] = Some[value]

object D
{
  def foo(bar : Int) : (Int, Option[Int]) => A =
  {
    bar match
    {
      case 1 => B.apply //return constructor of B
      case 2 => C.apply //return constructor of C
    }
  }
}

object HelloWorld {
  def main(args: Array[String]) {
    val b = D.foo(1)(1, 2)
    val c = D.foo(2)(1, None)

    println(b)
    println(c)
  }
}
走走:

abstract class A
case class B(foo : Int, bar : Option[Int]) extends A
case class C(foo : Int, bar : Option[Int]) extends A

implicit def int2option(value : Int) : Option[Int] = Some[value]

object D
{
  def foo(bar : Int) : (Int, Option[Int]) => A =
  {
    bar match
    {
      case 1 => B.apply //return constructor of B
      case 2 => C.apply //return constructor of C
    }
  }
}

object HelloWorld {
  def main(args: Array[String]) {
    val b = D.foo(1)(1, 2)
    val c = D.foo(2)(1, None)

    println(b)
    println(c)
  }
}
试试这个:

试试这个:


如何通过使用params的方法重写没有params的方法foo?此代码不应编译。谢谢您的建议。然而,我试图用构造函数来实现这一点,而不是用类的其他方法。在这里,您返回了一个未初始化的对象,然后将方法foo应用于该对象。我希望能够从apply方法返回构造函数,以便调用实体可以使用子类可能具有的任何参数初始化构造函数本身。您要求编译器在编译时预测在运行时将返回什么构造函数。这是不可能的。如何通过使用params的方法重写没有params的方法foo?此代码不应编译。谢谢您的建议。然而,我试图用构造函数来实现这一点,而不是用类的其他方法。在这里,您返回了一个未初始化的对象,然后将方法foo应用于该对象。我希望能够从apply方法返回构造函数,以便调用实体可以使用子类可能具有的任何参数初始化构造函数本身。您要求编译器在编译时预测在运行时将返回什么构造函数。这是不可能的。但是,现在不行。请不要这样做。它以什么方式不起作用?不编译?如果是,错误是什么?运行时失败?您正在尝试编写一个返回不同类型的函数。根据输入参数,两个构造函数的类型不同。虽然B和C可能是A的子类,但这并不意味着B和C的构造函数类型是A的子类型?因此,我不知道编译器如何对代码进行类型检查——它无法告诉您,当您调用foo1时,将返回B的构造函数,因此下一个参数应该与此兼容。更糟糕的是,您可以为某个变量调用D.foox,然后在运行时返回的类型会动态变化time@Paul我基本上希望它只返回基于bar值的正确构造函数,而不检查在编译时传递的参数是否兼容。Sergey Lagutin提供的代码就是这样做的。但是,我知道这不是完全安全的,因为它不会在编译时检查它,但如果传递了不正确的参数,它只会在运行时崩溃。OK。以这种方式放弃类型安全似乎是一种奇怪的设计,但当你整理你的床时,你也必须像陈词滥调那样躺在床上。然而,这现在不起作用,请不要这样做。它以什么方式不起作用?不编译?如果是,错误是什么?运行时失败?您正在尝试编写一个返回不同类型的函数。根据输入参数,两个构造函数的类型不同。虽然B和C可能是A的子类,但这并不意味着B和C的构造函数类型是A的子类型?因此,我不知道编译器如何对代码进行类型检查——它无法告诉您,当您调用foo1时,将返回B的构造函数,因此下一个参数应该与此兼容。更糟糕的是,您可以为某个变量调用D.foox,然后在运行时返回的类型会动态变化time@Paul我基本上希望它只返回基于bar值的正确构造函数,而不检查在编译时传递的参数是否兼容。Sergey Lagutin提供的代码就是这样做的。但是,我知道这不是完全安全的,因为它不会在编译时检查它,但如果传递了不正确的参数,它只会在运行时崩溃。OK。以这种方式放弃类型安全性似乎是一种奇怪的设计,但当你整理你的床时,你必须*躺在床上作为c
liche有。如果我传入错误类型的构造函数参数,会产生运行时错误?如果传入错误类型的构造函数参数,会产生运行时错误?当我知道参数的数量和类型时,这种错误会起作用。我一直在寻找可以扩展到任何数量的参数和任何类型的东西。当我知道参数的数量和类型时,这就行了。我一直在寻找可以扩展到任何数量的参数和任何类型的东西。