本文介绍了访问“父范围"在JShell中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎在另一个JShell中创建的JShell对象无权访问父级的JShell范围.例如:

It seems, that JShell object created inside another JShell does not have access to parent's JShell scope. For instance:

jshell> int x = 1;
x ==> 1

jshell> x
x ==> 1

jshell> jdk.jshell.JShell js = jdk.jshell.JShell.create();
js ==> jdk.jshell.JShell@1a052a00

jshell> js.eval("x");
$4 ==> [SnippetEvent(snippet=Snippet:ErroneousKey#1-x,previousStatus=NONEXISTENT,status=REJECTED,isSignatureChange=false,causeSnippetnull)]

jshell> js.eval("int x = 2;");
$5 ==> [SnippetEvent(snippet=Snippet:VariableKey(x)#2-int x = 2;,previousStatus=NONEXISTENT,status=VALID,isSignatureChange=true,causeSnippetnullvalue=2)]

jshell> js.eval("x");
$6 ==> [SnippetEvent(snippet=Snippet:ExpressionKey(x)#3-x,previousStatus=NONEXISTENT,status=VALID,isSignatureChange=true,causeSnippetnullvalue=2)]

是否有可能使父范围对孩子可见?

Is it somehow possible to make parent scope visible to the child one?

推荐答案

根据自己的 JVM中.

用于创建的Javadoc ()说:

当您遵循 build(),您会发现:

换句话说:最有可能的是,您正在创建另一个外壳运行的另一个 JVM实例.因此,至少目前为止:没有 child jshell知道其父级的机会.

In other words: most likely, you are creating another JVM instance where that other shell runs. So at least for now: no chances of having a child jshell know about its parent.

(如:我很希望Java的REPL功能可以在将来的某个时候将JShell附加到已经运行的JVM上)

( as in: I seriously hope that this REPL feature of Java will allow at some future point to attach a JShell to an already running JVM )

这篇关于访问“父范围"在JShell中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 19:16