通过反射检索方法时出现 NoSuchMethodException
NoSuchMethodException when retrieve method by reflection
我正在尝试通过名称获取 class 方法,例如:
case class A(i : Int) {def geti = i}
val a = A(123)
val met = a.getClass.getMethod("geti")
这似乎可行,但如果我尝试在 Double 上执行相同操作:
val a:Double = 3.0
a.getClass.getMethod("toString")
我运行进入这个错误:
java.lang.NoSuchMethodException: double.toString()
at java.lang.Class.getMethod(Class.java:1786)
你能帮我弄清楚我错过了什么吗?
来自 abstract final class scala.Double
的文档:
Double, a 64-bit IEEE-754 floating point number (equivalent to Java's
double primitive type) is a subtype of scala.AnyVal. Instances of
Double are not represented by an object in the underlying runtime
system.
There is an implicit conversion from scala.Double =>
scala.runtime.RichDouble which provides useful non-primitive
operations.
所以 scala Double
s 只有一个 toString()
方法,通过隐式转换为 RichDouble
,而 class Double
本身没有包含此方法。
我正在尝试通过名称获取 class 方法,例如:
case class A(i : Int) {def geti = i}
val a = A(123)
val met = a.getClass.getMethod("geti")
这似乎可行,但如果我尝试在 Double 上执行相同操作:
val a:Double = 3.0
a.getClass.getMethod("toString")
我运行进入这个错误:
java.lang.NoSuchMethodException: double.toString()
at java.lang.Class.getMethod(Class.java:1786)
你能帮我弄清楚我错过了什么吗?
来自 abstract final class scala.Double
的文档:
Double, a 64-bit IEEE-754 floating point number (equivalent to Java's double primitive type) is a subtype of scala.AnyVal. Instances of Double are not represented by an object in the underlying runtime system.
There is an implicit conversion from scala.Double => scala.runtime.RichDouble which provides useful non-primitive operations.
所以 scala Double
s 只有一个 toString()
方法,通过隐式转换为 RichDouble
,而 class Double
本身没有包含此方法。