我如何委托给 Scala 中的成员?

发布于 2024-10-28 05:32:02 字数 340 浏览 3 评论 0原文

在Scala中是否可以写这样的东西:

trait Road {
  ...
}

class BridgeCauseway extends Road {
  // implements method in Road
}

class Bridge extends Road {
  val roadway = new BridgeCauseway()

  // delegate all Bridge methods to the `roadway` member
}

或者我需要一一实现Road的每个方法,并在roadway上调用相应的方法?

Is it possible in Scala to write something like:

trait Road {
  ...
}

class BridgeCauseway extends Road {
  // implements method in Road
}

class Bridge extends Road {
  val roadway = new BridgeCauseway()

  // delegate all Bridge methods to the `roadway` member
}

or do I need to implement each of Road's methods, one by one, and call the corresponding method on roadway?

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

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

发布评论

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

评论(2

鯉魚旗 2024-11-04 05:32:02

实现此目的的最简单方法是使用隐式转换而不是类扩展:

class Bridge { ... }
implicit def bridge2road(b: Bridge) = b.roadway

只要您不需要携带原始的 Bridge (例如,您要存储Road 集合中的 Bridge)。

如果您确实需要再次取回 Bridge,您可以在 Road 中添加 owner 方法,该方法返回 Any >,使用 BridgeCauseway 的构造函数参数进行设置,然后进行模式匹配以获取桥梁:

trait Road {
  def owner: Any
  ...
}

class BridgeCauseway(val owner: Bridge) extends Road { . . . }

class Bridge extends Road {
  val roadway = new BridgeCauseway(this)
  ...
}

myBridgeCauseway.owner match {
  case b: Bridge => // Do bridge-specific stuff
  ...
}

The easiest way to accomplish this is with an implicit conversion instead of a class extension:

class Bridge { ... }
implicit def bridge2road(b: Bridge) = b.roadway

as long as you don't need the original Bridge to be carried along for the ride (e.g. you're going to store Bridge in a collection of Road).

If you do need to get the Bridge back again, you can add an owner method in Road which returns an Any, set it using a constructor parameter for BridgeCauseway, and then pattern-match to get your bridge:

trait Road {
  def owner: Any
  ...
}

class BridgeCauseway(val owner: Bridge) extends Road { . . . }

class Bridge extends Road {
  val roadway = new BridgeCauseway(this)
  ...
}

myBridgeCauseway.owner match {
  case b: Bridge => // Do bridge-specific stuff
  ...
}
爱殇璃 2024-11-04 05:32:02

如果您可以将Bridge作为特质,您就会被排序。

scala> trait A {
     |   val x: String
     | }
defined trait A

scala> class B extends A {
     |   val x = "foo"
     |   val y = "bar"
     | }
defined class B

scala> trait C extends A { self: B =>         
     |   val z = "baz"               
     | }
defined trait C

scala> new B with C
res51: B with C = $anon$1@1b4e829

scala> res51.x
res52: java.lang.String = foo

scala> res51.y
res53: java.lang.String = bar

scala> res51.z
res54: java.lang.String = baz

If you can make Bridge a trait you'll be sorted.

scala> trait A {
     |   val x: String
     | }
defined trait A

scala> class B extends A {
     |   val x = "foo"
     |   val y = "bar"
     | }
defined class B

scala> trait C extends A { self: B =>         
     |   val z = "baz"               
     | }
defined trait C

scala> new B with C
res51: B with C = $anon$1@1b4e829

scala> res51.x
res52: java.lang.String = foo

scala> res51.y
res53: java.lang.String = bar

scala> res51.z
res54: java.lang.String = baz
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文