Scala:我可以使用工厂方法重现匿名类的创建吗?

发布于 2024-12-07 18:20:05 字数 579 浏览 0 评论 0原文

据我了解,如果我使用 new 关键字创建一个类并在类名后面加上构造函数,Scala 就会创建一个匿名类:

class MyClass {
  def doStuff() { 
    // ... 
  }
}

val mc = new MyClass {
  doStuff()
}

好处是构造函数中的所有代码都在新对象的范围。

有没有办法可以重现这种语法,其中类是由工厂方法而不是 new 关键字创建的?即让下面的代码工作:

val mf = new MyFactory

val mc = mf.MyClass { 
  doStuff() 
}

我找不到一种方法来做到这一点,但 Scala 的功能非常丰富,这可能非常简单!

使用下面 @Ricky 建议的 import 我可以得到:(

val mf = MyFactory;
val mc = mf.MyClass

{
  import mc._
  doStuff()
}

需要块之前的空行)但该代码块不是构造函数。

As far as I understand it, Scala creates an anonymous class if I create a class using the new keyword and follow the class name with a constructor:

class MyClass {
  def doStuff() { 
    // ... 
  }
}

val mc = new MyClass {
  doStuff()
}

The nice thing being that all the code in the constructor is in the scope of the new object.

Is there a way I can reproduce this syntax where the class is created by a factory method rather than the new keyword? i.e. make the following code work:

val mf = new MyFactory

val mc = mf.MyClass { 
  doStuff() 
}

I can't find a way to do it but Scala has so much to it that this might be pretty easy!

Using an import as suggested by @Ricky below I can get:

val mf = MyFactory;
val mc = mf.MyClass

{
  import mc._
  doStuff()
}

(Where the blank line before the block is needed) but that code block is not a constructor.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稚气少女 2024-12-14 18:20:05

您可以这样做,但仍然必须保留 new 关键字,并将嵌套类创建为路径相关类型:

class Bippy(x: Int) {
  class Bop {
    def getIt = x
  }
}

val bip = new Bippy(7)
val bop = new bip.Bop

bop.getIt // yields 7

val bop2 = new bip.Bop{ override def getIt = 42 }

bop2.getIt // yields 42

You can do this, but you still have to keep the new keyword, and create the nested class as a path-dependent type:

class Bippy(x: Int) {
  class Bop {
    def getIt = x
  }
}

val bip = new Bippy(7)
val bop = new bip.Bop

bop.getIt // yields 7

val bop2 = new bip.Bop{ override def getIt = 42 }

bop2.getIt // yields 42
厌倦 2024-12-14 18:20:05

我认为这是不可能的。然而,一种常见的模式是向工厂方法添加一个参数,该方法采用修改创建的对象的函数:

trait MyClass {
  var name = ""
  def doStuff():Unit
}

class Foo extends MyClass {
  def doStuff() { println("FOO: " + name) }
}

trait MyClassFactory {
  def make: MyClass
  def apply( body: MyClass => Unit ) = {
    val mc = make
    body(mc)
    mc
  }
}

object FooFactory extends MyClassFactory {
  def make = new Foo
}

然后您可以使用与示例接近的语法创建和修改实例:

val foo = FooFactory { f=>
  f.name = "Joe"
  f.doStuff
}

I don't think it's possible. However, a common pattern is to add a parameter to factory methods which takes a function modifying the created object:

trait MyClass {
  var name = ""
  def doStuff():Unit
}

class Foo extends MyClass {
  def doStuff() { println("FOO: " + name) }
}

trait MyClassFactory {
  def make: MyClass
  def apply( body: MyClass => Unit ) = {
    val mc = make
    body(mc)
    mc
  }
}

object FooFactory extends MyClassFactory {
  def make = new Foo
}

You can then create and modify instance with a syntax close to your example:

val foo = FooFactory { f=>
  f.name = "Joe"
  f.doStuff
}
ぇ气 2024-12-14 18:20:05

听起来你只是想融入一种特质。 而不是调用 myFactoryMethod(classOf[Foo]] :

new T {
  override def toString = "My implementation here."
}

理想情况下,您可以编写

trait MyImplementation {
  override def toString = "My implementation here."
}

new Foo with MyImplementation

以下内容, 稳定标识符:

val foo = new Bar
import foo._
println(baz) //where baz is a member of foo.

It sounds like you're just looking to mix in a trait. Instead of calling myFactoryMethod(classOf[Foo]] which ideally would do (if Scala permitted it):

new T {
  override def toString = "My implementation here."
}

you can instead write

trait MyImplementation {
  override def toString = "My implementation here."
}

new Foo with MyImplementation

However, if you are just looking to get the members of the new object accessible without qualification, remember you can import from any stable identifier:

val foo = new Bar
import foo._
println(baz) //where baz is a member of foo.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文