Scala:如何使用自定义属性扩展第三方类

Scala:如何使用自定义属性扩展第三方类,scala,Scala,我需要用一个属性扩展一个外部类。。。由于dynamic在这种情况下无法应用,我没有其他选择,只能创建如下包装: class MyWrapper(val otherClass: OtherClass, val myAttribute: Int) { ... } val myWrapper = new MyWrapper(new OtherClass, 1) myWrapper.otherClass.foo 然后,我像这样调用OtherClass成员: class MyWrapper(v

我需要用一个属性扩展一个外部类。。。由于
dynamic
在这种情况下无法应用,我没有其他选择,只能创建如下包装:

class MyWrapper(val otherClass: OtherClass, val myAttribute: Int) {

  ...
}

val myWrapper = new MyWrapper(new OtherClass, 1)
myWrapper.otherClass.foo
然后,我像这样调用
OtherClass
成员:

class MyWrapper(val otherClass: OtherClass, val myAttribute: Int) {

  ...
}

val myWrapper = new MyWrapper(new OtherClass, 1)
myWrapper.otherClass.foo
打这样的电话有什么诀窍吗

myWrapper.foo
我尝试了
apply
。。。但它不起作用:

class MyWrapper(val otherClass: OtherClass, val myAttribute: Int) {

  def apply = otherClass
}
此外,我不能像这样扩展
OtherClass

class MyWrapper(val myAttribute: Int) extends OtherClass {

  ...
}
。。。因为我需要使用一个从字节数组反序列化它的工厂类。

您可以使用该模式(我相信他们试图淡化名称)。基本上,您可以使用隐式实现这一点。事实上,您可以使用as of 2.10

object Pimper{
  implicit class WrapperClass(wrapped: OtherClass){
    def additionalStuff....
  }
}

import Pimper._
new OtherClass().additionalStuff
关于隐式的内容已经很多了,所以我让你自己研究一下剩下的:)

你可以使用这个模式(我相信他们试图淡化这个名字)。基本上,您可以使用隐式实现这一点。事实上,您可以使用as of 2.10

object Pimper{
  implicit class WrapperClass(wrapped: OtherClass){
    def additionalStuff....
  }
}

import Pimper._
new OtherClass().additionalStuff
关于隐式的内容已经很多了,所以我让你自己研究一下剩下的:)

你可以使用这个模式(我相信他们试图淡化这个名字)。基本上,您可以使用隐式实现这一点。事实上,您可以使用as of 2.10

object Pimper{
  implicit class WrapperClass(wrapped: OtherClass){
    def additionalStuff....
  }
}

import Pimper._
new OtherClass().additionalStuff
关于隐式的内容已经很多了,所以我让你自己研究一下剩下的:)

你可以使用这个模式(我相信他们试图淡化这个名字)。基本上,您可以使用隐式实现这一点。事实上,您可以使用as of 2.10

object Pimper{
  implicit class WrapperClass(wrapped: OtherClass){
    def additionalStuff....
  }
}

import Pimper._
new OtherClass().additionalStuff
关于隐式的研究已经很多了,所以我将让您自己研究其余的:)