我有一個groovy類,它查找方法引用然後調用它。被調用的方法是一種私有方法。當實際的類是子類的一個實例時,它會拋出一個錯誤,即它無法找到私有方法,即使它是父級中的實際調用它的公共方法。使用方法引用時的groovy方法範圍
在這種情況下,我顯然可以直接調用pMethod2()
並且工作正常,但我試圖理解爲什麼這不能像寫入那樣工作,以及是否有方法糾正它,以便它能正常工作。
class Parent {
def pMethod1() {
def m = this.&pMethod2
m() // this call fails if the calling class is of type Child
}
private def pMethod2() {}
public static void main(String[] args) {
new Child().pMethod1();
}
}
class Child extends Parent {}