scala 有没有办法扩展对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7625040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:31:20  来源:igfitidea点击:

Is there any way to extend an object?

scala

提问by Jus12

In scala, we cannot extend object:

在 Scala 中,我们不能扩展object

object X 
object Y extends X

gives an error error: not found: type X

给出错误 error: not found: type X

In my case someone has defined some functionality in an object and I need to extend it (basically add another method). What would be the easiest way to extend this object?

在我的例子中,有人在一个对象中定义了一些功能,我需要扩展它(基本上是添加另一个方法)。扩展此对象的最简单方法是什么?

回答by Jens Schauder

As so often the correct answer depends on the actual business requirement. Extending from an object would in some sense defy the purpose of that object since it wouldn't be a singleton any longer.

正确答案通常取决于实际业务需求。从某个对象扩展在某种意义上会违背该对象的目的,因为它不再是单例了。

What might be a solution is to extract the behavior into an abstract trait. And create objects extending that trait like so:

可能的解决方案是将行为提取为抽象特征。并创建扩展该特性的对象,如下所示:

trait T{
    // some behavior goes here
}

object X extends T

object Y extends T {
    // additional stuff here
}

回答by YoK

If you want use methods and values from another object you can use import.

如果您想使用来自另一个对象的方法和值,您可以使用导入。

object X{
  def x = 5
}

object Y{
  import X._
  val y = x
}

回答by Owen

You can't actually extend an object, because that would create two of it, and an object by definition exists only once (edit: well, that's not quite true, because the object definition can be in a class or method).

你实际上不能扩展一个对象,因为那会创建两个对象,并且一个对象根据定义只存在一次(编辑:嗯,这并不完全正确,因为对象定义可以在一个类或方法中)。

For your purposes, try this:

为了你的目的,试试这个:

object X {
}

object Y {
    def a = 5
}

implicit def xToY(x: X.type) = Y

println(X.a)

It doesn't actually extend, but it does allow you to call new methods on it than were originally defined.

它实际上并没有扩展,但它确实允许您在其上调用比最初定义的新方法。

回答by Mirco Dotta

The only way to share code between two objects is by having one or more common superclass/trait.

在两个对象之间共享代码的唯一方法是拥有一个或多个共同的超类/特征。

回答by Xavier Guihot

Note that with Dotty(foundation of Scala 3), you can alternatively use composition (instead of inheritance) via export clauseswhich allow defining aliases for selected members of an object:

请注意,使用Dotty(foundation of Scala 3),您还可以通过导出子句使用组合(而不是继承),这些子句允许为对象的选定成员定义别名:

object X { def f = 5 }

object Y {
  export X._
  def g = 42
  def h = f * g
}

Y.f // 5
Y.g // 42
Y.h // 210

Note that you can also restrict which members you want to export:

请注意,您还可以限制要导出的成员:

object X { def f = 5; def g = 6 }
object Y { export X.f }
Y.f // 5
Y.g
^^^
value g is not a member of Y

回答by Stevo Slavi?

You can convert parent into class + companion object, and then have child extend class E.g.

您可以将父对象转换为类 + 伴生对象,然后让子对象扩展类 Eg

in Parent.scala

在 Parent.scala 中

class Parent {}

object Parent extends Parent {}

And then in Child.scala

然后在 Child.scala

object Child extends Parent {}

Yes, it's more a hack than a solution.

是的,这与其说是解决方案,不如说是一种黑客攻击。