简体   繁体   English

Scala中有诸如类方法之类的东西吗?

[英]Are there such thing as class methods in Scala?

Many oo languages have some facility for class methods and class variables. 许多oo语言为类方法和类变量提供了一定的便利。 Ruby for example allows this with metaclasses. 例如Ruby允许使用元类。 Is it fair to look at companion objects in a similar fashion in Scala? 在Scala中以类似的方式查看伴侣对象是否公平?

Is it fair to looks at companion objects in a similar fashion in Scala? 在Scala中以类似的方式查看伴侣对象是否公平?

Yes, that's a reasonable way to think about them. 是的,这是考虑它们的合理方法。

On JVM classes are not objects in the same sense as in Ruby or other dynamic languages. 在JVM上,类的含义不同于在Ruby或其他动态语言中的对象。 Sure, you can get representation of a class as a special object of class Class[_] , but it is used primarily for reflection and cannot have custom methods, for example. 当然,您可以将类的表示形式表示为类Class[_]的特殊对象,但是它主要用于反射,例如,不能具有自定义方法。 However, JVM has notion of static methods and fields, which are associated with a class and does not require objects of the class to be used: 但是,JVM具有与类相关联的静态方法和字段的概念,并且不需要使用该类的对象:

public class SomeClass 
    public static int add(int x, int y) { return x + y; }
}

public class OtherClass {
    public static void main() {
        int z = SomeClass.add(1, 2);
    }
}

These methods and fields are essentially "global" (unless we're considering class loaders mechanism), and methods, for example, cannot be overridden in subclasses. 这些方法和字段本质上是“全局”的(除非我们考虑使用类加载器机制),例如,方法不能在子类中覆盖。

Scala does not let you define and use statics directly - there is no notion of "static" in the language. Scala不允许您直接定义和使用静态-语言中没有“静态”的概念。 However, in Scala you can define singleton objects which can be used to emulate static fields and methods and at the same time are much more powerful than statics, for example, they can participate in inheritance. 但是,在Scala中,您可以定义可用于模拟静态字段和方法的单例对象,同时它们比静态函数强大得多,例如,它们可以参与继承。 Internally these objects are instances of special class generated by Scala compiler for you. 在内部,这些对象是Scala编译器为您生成的特殊类的实例。

Also Scala has special kind of these singleton objects, called companion objects, which share the name with some class: Scala还具有一种特殊的此类单例对象,称为伴侣对象,它们与某些类共享名称:

class SomeClass

object SomeClass

Internally these objects are instances of completely different class than SomeClass (it can be accessed as SomeClass.type ), but Scala treats them in special way, for example, you can access SomeClass private members from SomeClass companion object and vice versa. 国内这些对象是完全不同的类比的情况下SomeClass (它可以作为访问SomeClass.type ),但斯卡拉对待他们特殊的方式,例如,你可以访问SomeClass私人会员SomeClass同伴对象,反之亦然。 Also companion objects provide additional implicit scope. 伴随对象还提供其他隐式作用域。 However, SomeClass.type and SomeClass are completely different classes. 但是, SomeClass.typeSomeClass是完全不同的类。

In short, yes, you can think of companion objects as containers for "class" methods and fields, but only to some extent. 简而言之,是的,您可以将伴随对象视为“类”方法和字段的容器,但仅在某种程度上。 Companion objects are not metaclasses in any way; 伴侣对象无论如何都不是元类。 they have completely different semantics and implementation. 它们具有完全不同的语义和实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM