Scala 中的静态内部类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1069987/
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 01:29:57  来源:igfitidea点击:

Static inner classes in scala

javascalastaticinner-classes

提问by oxbow_lakes

What is the analog in Scala of doing this in Java:

Scala 中在 Java 中执行此操作的模拟是什么:

public class Outer {
  private Inner inner;

  public static class Inner {
  }

  public Inner getInner() { return inner; }
}

I specifically want my inner class to nothave to have a fully qualified name - i.e. I want Trade.Type, not TradeType. So in Scala I imagined it might be something like:

我特别希望我的内部类不必有一个完全限定的名称 - 即我想要Trade.Type,不是TradeType。所以在 Scala 中,我想象它可能是这样的:

class Outer(val inner: Inner) {
    object Inner
}

But this doesn't seem to work: my scala Innerjust doesn't seem to be visible from outside the Outerclass. One solution would of course be:

但这似乎不起作用:我的 ScalaInner似乎从Outer课堂外看不到。一种解决方案当然是:

class Inner
class Outer(val inner: Inner)

Which is OK - but because of the names of my classes, Inneris really the "type" of the Outerand Outeractually has a long name. So:

没关系 - 但是由于我的类的名称,Inner实际上是 的“类型”Outer并且Outer实际上具有很长的名称。所以:

class SomeHorriblyLongNameType
class SomeHorriblyLongName(myType: SomeHorriblyLongNameType)

Which is verbose and horrible. I could replace SomeHorriblyLongNameTypewith just Typebut there would then be no obvious connection between it and the class it was related to. Phew

这是冗长而可怕的。我可以SomeHorriblyLongNameType用 just替换,Type但是它和它相关的类之间不会有明显的联系。呼

回答by agilefall

You can do something like this if don't need access to the outer class in the inner class (which you wouldn't have in Java given that your inner class was declared static):

如果不需要访问内部类中的外部类,您可以执行以下操作(鉴于您的内部类已声明,您在 Java 中不会这样做static):

object A{
    class B {
        val x = 3
    }
}
class A {
    // implementation of class here
}
println(new A.B().x)

回答by Jorge Ortiz

As others have pointed out, "static" classes should be placed inside the companion object.

正如其他人指出的那样,“静态”类应该放置在伴生对象中。

In Scala, classes, traits, and objects which are members of a class are path-dependent. For example:

在 Scala 中,作为类成员的类、特征和对象是路径相关的。例如:

class Button {
  class Click
}


val ok = new Button
val cancel = new Button

val c1 = new ok.Click
val c2 = new cancel.Click

Now c1 and c2 are instances of -different- classes. One class is ok.Click, and the other is cancel.Click. If you wanted to refer to the type of all Click classes, you could say Button#Click.

现在 c1 和 c2 是 -不同类的实例。一类是ok.Click,另一类是cancel.Click。如果你想引用所有 Click 类的类型,你可以说 Button#Click。

回答by skaffman

From scala-lang:

从 Scala-lang

there is no notion of 'static' members in Scala. Instead, Scala treats static members of class Y as members of the singleton object Y

Scala 中没有“静态”成员的概念。相反,Scala 将类 Y 的静态成员视为单例对象 Y 的成员

So it seems you could have a class defined inside an Object, but not a static class defined inside a class.

因此,您似乎可以在 Object 中定义一个类,但不能在类中定义一个静态类。

回答by Alexander Azarov

Not sure I fully understood your use case... If it can help you, objects inside classes are visible like an instance's fields, e.g.

不确定我是否完全理解你的用例......如果它可以帮助你,类中的对象就像实例的字段一样可见,例如

case class C(var x: Int)
class A { case object b extends C(0) }
val a = new A
println(a.b.x)
a.b.x = 2
println(a.b.x)

Moreover, you can perfectly override a parent's val with object:

此外,您可以使用对象完美地覆盖父级的 val:

case class C(var x: Int)
class A { val y: C = C(0) }
class B extends A { override case object y extends C(2) }
val a: A = new B
println(a.y.x)

回答by Hugo Delgado

In scala if you need to create a some static methods you can use a companion object, with the same name of the class, where you store all the pseudo static methods. Ex:

在 scala 中,如果您需要创建一些静态方法,您可以使用一个伴生对象,该对象具有相同的类名,您可以在其中存储所有伪静态方法。前任:

class A {
}

object A {
    def xpto // define some pseudo static methods here..
}

Then you can just use A.xpto.
Try to read more about companion modules on scala

然后你就可以使用A.xpto.
尝试在 Scala 上阅读有关配套模块的更多信息