簡體   English   中英

intellij中不推薦使用的ReplaceWith在Kotlin中如何工作?

[英]How does Deprecated ReplaceWith work for Kotlin in intellij?

我寫了這段代碼:

@Deprecated("Old stuff", ReplaceWith("test2"))
fun test1(i: Int) {
    println("old Int = $i")
}

fun test2(i: Int) {
    println("new Int = $i")
}

fun main(args: Array<String>) {
    test1(3)
}

由於某種原因,當我按Alt + Enter並單擊“用test2替換”時,方法test1消失並且沒有被替換,我在做什么錯?

編輯:

它確實適用於類:

@Deprecated("Old stuff", ReplaceWith("Test2"))
class Test1
class Test2

fun main(args: Array<String>) {
    val a = Test1()
}

您需要告訴您如何精確地替換它...雖然我不知道為什么它被完全刪除了,但我將向您展示我的意思:

如果要改用以下內容:

@Deprecated("Old stuff", ReplaceWith("test2(i)"))

它將正確替換您對test2(5) test1(5)調用。

還要注意,有時如果不清楚應該進行哪個替換,則可能還需要添加軟件包名稱,例如:

@Deprecated("Old stuff", ReplaceWith("org.example.test2(i)"))
// or just use:
@Deprecated("Old stuff", ReplaceWith("test2(i)", /* here come the imports */ "org.example.test2"))

如果需要,還可以在替換中使用靜態值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM