scala 中的静态内部类

发布于 2024-07-26 11:20:56 字数 903 浏览 6 评论 0原文

Scala 中与 Java 中执行此操作的类似之处是什么:

public class Outer {
  private Inner inner;

  public static class Inner {
  }

  public Inner getInner() { return inner; }
}

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

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

但这似乎不起作用:我的 scala Inner 只是似乎从 Outer 类外部不可见。 一种解决方案当然是:

class Inner
class Outer(val inner: Inner)

哪个都可以 - 但由于我的类的名称,Inner实际上是OuterOuter<的“类型” /code> 实际上有一个很长的名字。 所以:

class SomeHorriblyLongNameType
class SomeHorriblyLongName(myType: SomeHorriblyLongNameType)

这是冗长和可怕的。 我可以将 SomeHorriblyLongNameType 替换为 Type,但它和与之相关的类之间就没有明显的联系。 唷

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

public class Outer {
  private Inner inner;

  public static class Inner {
  }

  public Inner getInner() { return inner; }
}

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

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

But this doesn't seem to work: my scala Inner just doesn't seem to be visible from outside the Outer class. One solution would of course be:

class Inner
class Outer(val inner: Inner)

Which is OK - but because of the names of my classes, Inner is really the "type" of the Outer and Outer actually has a long name. So:

class SomeHorriblyLongNameType
class SomeHorriblyLongName(myType: SomeHorriblyLongNameType)

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

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

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

发布评论

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

评论(5

允世 2024-08-02 11:20:56

如果不需要访问内部类中的外部类(由于您的内部类被声明为static,因此在 Java 中就不需要访问),您可以执行以下操作:

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

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):

object A{
    class B {
        val x = 3
    }
}
class A {
    // implementation of class here
}
println(new A.B().x)
极度宠爱 2024-08-02 11:20:56

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

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

class Button {
  class Click
}


val ok = new Button
val cancel = new Button

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

现在 c1 和 c2 是不同类的实例。 一类是ok,一类是cancel,一类是cancel,点击一下。 如果您想引用所有 Click 类的类型,您可以说 Button#Click。

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:

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.

指尖上得阳光 2024-08-02 11:20:56

来自 scala-lang

没有“静态”成员的概念
在斯卡拉。 相反,Scala 对待
类 Y 的静态成员作为成员
单例对象Y的

所以看来你可以在一个对象中定义一个类,但不能在一个类中定义一个静态类。

From 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

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

灼痛 2024-08-02 11:20:56

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

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)

此外,你可以用对象完美地覆盖父级的 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)

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:

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)
塔塔猫 2024-08-02 11:20:56

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

class A {
}

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

那么您可以使用A.xpto
尝试阅读有关 scala 上的配套模块的更多信息

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:

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

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