次の例を想定 - リターンを実行したとき - 「fatherfather」デフォルトパラメータで形質メソッドをオーバーライド
trait Action {
def doSomething(s:String = "father") = s
}
class RevokeAction extends Action{
override def doSomething(s:String) = {
s + s
}
}
object HelloWorld {
def main(args: Array[String]): Unit = {
val revokeAction = new RevokeAction()
revokeAction.doSomething()
}
}
コンパイラが何をするかを見てみると -
package <empty> {
abstract trait Action extends Object {
def doSomething(s: String): String = s;
<synthetic> def doSomething$default$1(): String = "father";
def /*Action*/$init$(): Unit = {
()
}
};
class RevokeAction extends Object with Action {
<synthetic> def doSomething$default$1(): String = RevokeAction.super.doSomething$default$1();
override def doSomething(s: String): String = s.+(s);
def <init>(): RevokeAction = {
RevokeAction.super.<init>();
RevokeAction.super./*Action*/$init$();
()
}
};
object HelloWorld extends Object {
def main(args: Array[String]): Unit = {
val revokeAction: RevokeAction = new RevokeAction();
{
revokeAction.doSomething(revokeAction.doSomething$default$1());
()
}
};
def <init>(): HelloWorld.type = {
HelloWorld.super.<init>();
()
}
}
}
に何が起こっているのより良い説明を与えます
これが期待される動作である理由を詳しく教えてください。 スカラ、デフォルトのパラメータで
仕様でその見落としがありました - ありがとうございました! –
"を宣言クラスの定数メンバとして"メソッドを意味すると思います。 –
@AlexeyRomanov "declaring class"とは、デフォルトのパラメータを持つメソッドが宣言されたクラスを意味します。 –