在普通的Java中,我可以写
class P {
static <A> A id (A x) { return x; }
static int y = P.<Integer>id(8);
static String bar = P.<String>id("foo");
}
在jshell中,我可以声明并使用 id
jshell> <A> A id (A x) { return x; }
| created method id(A)
jshell> int x = id(8)
x ==> 8
jshell> String y = id("foo")
y ==> "foo"
但我没有看到如何使类型参数明确 .
jshell> String y = <String>id("foo")
| Error:
| illegal start of expression
| String y = <String>id("foo");
| ^
隐含上下文类的名称是什么?
(允许我回答这个问题的jshell规范的一部分)在哪里? http://openjdk.java.net/jeps/222刚刚在"the wrapping"中提到"synthetic class" . 听起来不像它的名字 .
1 回答
实际上,你的link没有指定将你的方法作为静态方法的syntetic类的确切性质(如名称) .
我试图获取该片段正在执行的类
但正如您所看到的,信息不在堆栈跟踪中 .
绕过它的最简单方法是将方法定义为自己类中的静态方法:
这允许你打电话
并获得所需的结果 .