如何在 Scala 中将方法限定为静态方法? [英] How to qualify methods as static in Scala?

查看:49
本文介绍了如何在 Scala 中将方法限定为静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有课

class MyClass {
  def apply(myRDD: RDD[String]) {
      val rdd2 = myRDD.map(myString => {
          // do String manipulation
      }
  }

}

object MyClass {

}

由于我有一段代码执行一项任务(该区域显示 执行字符串操作"),我认为我应该将其分解为它自己的方法.由于该方法不会更改类的状态,因此我认为应该将其设为 static 方法.

Since I have a block of code performing one task (the area that says "do String manipulation"), I thought I should break it out into it's own method. Since the method is not changing the state of the class, I thought I should make it a static method.

我该怎么做?

我认为你可以在伴随对象中弹出一个方法,它可以作为静态类使用,如下所示:

I thought that you can just pop a method inside the companion object and it would be available as a static class, like this:

object MyClass {
  def doStringManipulation(myString: String) = {
    // do String manipulation
  }
}

但是当我尝试 val rdd2 = myRDD.map(myString => { doStringManipulation(myString)}) 时,scala 无法识别该方法并迫使我执行 MyClass.doStringManipulation(myString) 以调用它.

but when I try val rdd2 = myRDD.map(myString => { doStringManipulation(myString)}), scala doesn't recognize the method and it forces me to do MyClass.doStringManipulation(myString) in order to call it.

我做错了什么?

推荐答案

在 Scala 中没有 静态 方法:所有方法都定义在一个对象上,无论是类的实例还是单例,正如您在问题中定义的那样.

In Scala there are no static methods: all methods are defined over an object, be it an instance of a class or a singleton, as the one you defined in your question.

正如您正确指出的那样,通过在同一编译单元中以相同方式命名一个 class 和一个 object,您可以使对象成为 companion ,这意味着两者可以访问彼此的 private 字段和方法,但这确实意味着它们可以在不指定您正在访问的对象的情况下使用.

As you correctly pointed out, by having a class and an object named in the same way in the same compilation unit you make the object a companion of the class, which means that the two have access to each others' private fields and methods, but this does mean they are available without specifying which object you are accessing.

您想要做的是使用前面提到的长格式 (MyClass.doStringManipulation(myString)),或者,如果您认为有意义,您可以在 class'作用域,如下:

What you want to do is either using the long form as mentioned (MyClass.doStringManipulation(myString)) or, if you think it makes sense, you can just import the method in the class' scope, as follows:

import MyClass.doStringManipulation

class MyClass {
  def apply(myRDD: RDD[String]): Unit = {
    val rdd2 = myRDD.map(doStringManipulation)
  }
}

object MyClass {
  private def doStringManipulation(myString: String): String = {
    ???
  }
}

作为旁注,对于 MyClass.apply 方法,您使用了将来会消失的符号:

As a side note, for the MyClass.apply method, you used the a notation which is going to disappear in the future:

// this is a shorthand for a method that returns `Unit` but is going to disappear
def method(parameter: Type) {
  // does things
}

// this means the same, but it's going to stay
// the `=` is enough, even without the explicit return type
// unless, that is, you want to force the method to discard the last value and return `Unit`
def method(parameter: Type): Unit = {
  // does things
}

这篇关于如何在 Scala 中将方法限定为静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆