代码之家  ›  专栏  ›  技术社区  ›  Sharp Steel Software

未找到隐式类方法

  •  0
  • Sharp Steel Software  · 技术社区  · 7 年前

    我有一个隐式类的对象:

    object ModelUtils {
      implicit class RichString(str: String) {
        def isNullOrEmpty(x: String): Boolean = x == null || x.trim.isEmpty
      }
    }
    

    但是,当我尝试使用它时,IntelliJ找不到isNullOrEmpty方法:

    "TestString".isNullOrEmpty
    

    我尝试了各种导入方法,但都没有成功。我错过了什么?

    1 回复  |  直到 7 年前
        1
  •  5
  •   Andrey Tyukin    7 年前

    问题可能不在于导入本身,而在于不必要的参数 x . 如果你想打电话 .isNullOrEmpty str 十、

    object ModelUtils {
      implicit class RichString(str: String) {
        def isNullOrEmpty: Boolean = str == null || str.trim.isEmpty
      }
    }
    
    import ModelUtils._
    
    println("TestString".isNullOrEmpty)
    
    推荐文章