1
最近我遇到過Groovy不允許強制到最終類。爲什麼groovy不會強制到最後的類
final class Example {
def x
}
// GroovyCastException. cannot coerce because it is final class
def a = { x = 5 } as Example
這是爲什麼?我想這是Groovy用於強制類的機制的東西嗎?
最近我遇到過Groovy不允許強制到最終類。爲什麼groovy不會強制到最後的類
final class Example {
def x
}
// GroovyCastException. cannot coerce because it is final class
def a = { x = 5 } as Example
這是爲什麼?我想這是Groovy用於強制類的機制的東西嗎?
你的代碼,但工作是:
@groovy.transform.ToString(includeNames=true)
final class Example {
def x
}
def a = [x:5] as Example
println a // Example(x:5)
要添加到什麼蒂姆·耶茨發佈,這就是問題所在:
def a = { x = 5 } as Example
之所以這樣工作的:
def a = [x:5] as Example
是因爲它創建了一個值爲5的映射,Groovy運行時可以獲取該映射的屬性並將其分配給Example
屬性具有相同的密鑰。在該閉包中發生的所有事情都是一個名爲X的變量被設置在其範圍內,但不是可以在類型強制期間傳送的某個對象的屬性。
那麼它仍然沒有幫助我進口的最終課程。 – lapots
@ user1432980不知道這意味着什麼? –
它不能爲預編譯的外部類創建代理? – lapots