【发布时间】:2026-01-13 13:15:01
【问题描述】:
我正在尝试使用反射,与this question 的答案中所见的方式几乎相同。问题是我的代码如下所示:
class A {
def query1(arg: Int): String = { Some code }
def query2(arg: String): String = { Some code }
def query3(): String = { Some code }
}
object A {
def apply(query: String, arg: Any): String = {
val a = new A
val method = a.getClass.getMethod(query,arg.getClass)
method.invoke(a,arg)
}
}
但这无法编译,我得到一个错误:
type mismatch; found: Any, required: Object
有什么想法可以让我完成这项工作吗?
【问题讨论】:
标签: scala reflection