我只是在玩 JShell,似乎定义class Z{}然后定义
var z = new Z()不起作用。但是使用不同的类名,比如class Xand class A,确实有效。
当然我一定遗漏了一些明显的东西......?
| Welcome to JShell -- Version 14.0.1
| For an introduction type: /help intro
jshell> class X{}
| created class X
jshell> class Z{}
| created class Z
jshell> var x = new X()
x ==> X@26a1ab54
| created variable x : X
jshell> var z = new Z()
| Error:
| unexpected type
| required: class
| found: type parameter Z
| var z = new Z();
| ^
jshell> class A{}
| created class A
jshell> var a = new A()
a ==> A@2ef1e4fa
| created variable a : A
Run Code Online (Sandbox Code Playgroud)
Joe*_*Joe 10
的使用var可能导致变量具有不可表示的类型。例如,查看可能是Stringor的表达式的返回类型Integer:
jshell> /set feedback verbose
jshell> var x = true ? "a" : 1
x ==> "a"
| created variable x : Serializable&Comparable<? extends Serializable&Comparable<?>&java.lang.constant.Constable&java.lang.constant.ConstantDesc>&java.lang.constant.Constable&java.lang.constant.ConstantDesc
Run Code Online (Sandbox Code Playgroud)
在jshell评估您的代码片段时,如果是这种情况,它会将其包装在一个代码块中,以便它可以记录此类型以供以后使用。包装片段包括一个名为 的泛型类型参数Z:
// private static <Z> Z do_itAux() {
// wtype x_ = y;
// @SuppressWarnings("unchecked")
// Z x__ = (Z) x_;
// return x__;
Run Code Online (Sandbox Code Playgroud)
此参数的名称会泄漏到正在评估的代码块中,这意味着该类的名称会被类型参数所掩盖。这是Z一个特例——其他单字符示例也很好。
| 归档时间: |
|
| 查看次数: |
193 次 |
| 最近记录: |