代码之家  ›  专栏  ›  技术社区  ›  Coder-Man

不推荐的ReplaceWith如何在intellij中为Kotlin工作?

  •  2
  • Coder-Man  · 技术社区  · 7 年前

    @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()
    }
    
    1 回复  |  直到 7 年前
        1
  •  28
  •   Roland    7 年前

    如果您使用以下选项:

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

    它将取代你的 test1(5) test2(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"))
    

    您还可以在替换中使用静态值,以防需要。