1
我想为类型为Example
的对象添加对类String
的转换。从字符串到某个对象的自定义类型转换
当我做这样的
class Example {
def x = 5
}
class ExampleConversionCategory {
static def support = String.&asType
static Object asType(String self, Class cls) {
if (cls == Example.class) {
"convert"
} else { support(cls) } // argument type mismatch
}
}
String.mixin(ExampleConversionCategory)
def x = "5" as int
println x
我得到异常
java.lang.IllegalArgumentException: argument type mismatch
问题是什么? cls
有Class
类型。