[英]what is the “self” meaning in new class of scala
recently I am reading the source of spark. 最近,我正在阅读火花的来源。 When reaching to the class of "org.apache.spark.deploy.SparkSubmit", I got confusion about the keyword "self" and the operator "=>".
当到达“ org.apache.spark.deploy.SparkSubmit”的类时,我对关键字“ self”和运算符“ =>”感到困惑。 Is anyone can explain me for that?
有人可以为我解释吗?
override def main(args: Array[String]): Unit = {
val submit = new SparkSubmit() {
self =>
override protected def parseArguments(args: Array[String]): SparkSubmitArguments = {
new SparkSubmitArguments(args) {
override protected def logInfo(msg: => String): Unit = self.logInfo(msg)
override protected def logWarning(msg: => String): Unit = self.logWarning(msg)
}
}
override protected def logInfo(msg: => String): Unit = printMessage(msg)
override protected def logWarning(msg: => String): Unit = printMessage(s"Warning: $msg")
override def doSubmit(args: Array[String]): Unit = {
try {
super.doSubmit(args)
} catch {
case e: SparkUserAppException =>
exitFn(e.exitCode)
case e: SparkException =>
printErrorAndExit(e.getMessage())
}
}
}
BTW: this question is totally different from the “duplicated one”. 顺便说一句:这个问题与“重复的”完全不同。 Although these two are very same, what i am asking is about the “self =>” near the key word of “new class” rather than the “duplicated” with “ some name =>” in the class definition of scala.
尽管这两者是非常相同的,但我要问的是关于“新类”关键字附近的“自我=>”,而不是在scala的类定义中与“重复的名称”相似的“重复”。 it's not a same question
这不是一个相同的问题
The statement 该声明
self =>
is called a "self type annotation" and it creates a value named self
that refers to the instance of the class being constructed. 称为“自我类型注释”,它创建一个名为
self
的值,该值引用正在构造的类的实例。 This can be used in places where the this
value for the class is not available. 可以在无法使用该类的
this
值的地方使用它。 In particular, it can be used inside a nested class, where this
refers to the nested class and a reference to the outer class is not automatically available. 特别地,它可以在嵌套类内被使用,其中
this
指的是嵌套类和到外类的引用不是自动可用。
In your case, self
is used here: 在您的情况下,此处使用
self
:
new SparkSubmitArguments(args) {
override protected def logInfo(msg: => String): Unit = self.logInfo(msg)
override protected def logWarning(msg: => String): Unit = self.logWarning(msg)
}
This makes the new instance of SparkSubmitArguments
use the logInfo
and logWaringing
methods from the outer, containing class. 这使得
SparkSubmitArguments
的新实例使用外部包含类的logInfo
和logWaringing
方法。 You can't use this
at this point of the code because it would refer to the inner class, not the outer class. 您现在不能在代码上使用
this
,因为它将引用内部类,而不是外部类。 (If you do use this
here you will get an infinite loop) (如果您在此处使用
this
,则会出现无限循环)
It's an alias for this
. 这是一个别名
this
。 This is done to disambiguate self-reference in inner classes. 这样做是为了消除内部类中的自引用。
When you use this
in the scope of an inner class, it refers to the inner class' instance. 当您在内部类的范围内使用
this
时,它引用内部类的实例。 If you need a reference to the outer class, you would need an alias though: 如果需要引用外部类,则需要一个别名:
class Foo { self =>
val x = 1
new AnyRef {
val x = 2
println(this.x) // 2
println(self.x) // 1
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.