scala 中的静态内部类
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
实际上是Outer
和Outer<的“类型” /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果不需要访问内部类中的外部类(由于您的内部类被声明为
static
,因此在 Java 中就不需要访问),您可以执行以下操作: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
):正如其他人指出的那样,“静态”类应该放置在伴随对象中。
在 Scala 中,作为类成员的类、特征和对象是路径相关的。 例如:
现在 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:
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.
来自 scala-lang:
所以看来你可以在一个对象中定义一个类,但不能在一个类中定义一个静态类。
From scala-lang:
So it seems you could have a class defined inside an Object, but not a static class defined inside a class.
不确定我完全理解你的用例...如果它可以帮助你,类内的对象就像实例的字段一样可见,例如
此外,你可以用对象完美地覆盖父级的 val :
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.
Moreover, you can perfectly override a parent's val with object:
在 scala 中,如果您需要创建一些静态方法,您可以使用一个与类同名的伴随对象,在其中存储所有伪静态方法。 例如:
那么您可以使用
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:
Then you can just use
A.xpto
.Try to read more about companion modules on scala