这个问题来自SCJP转储。也许这听起来很愚蠢,但我对选择有些困惑。请帮帮我
公共类Donkey2 { 公共静态void (String[] args) {布尔值assertsOn =真;断言( assertsOn ):assertsOn= true;if(assertsOn) { System.out.println("assert on");} }
如果类Donkey被两次调用,第一次没有启用断言,第二次在启用断言时,结果是什么?
A.无产出
B.没有输出;断言已启动
C.断言正在进行
没有输出;抛出一个AssertionError。
E.断言已启动;抛出一个AssertionError
(回答)如果我调用它两次,我将得到断言在断言上,断言是打开的
这是真的吗?
书上说答案是C),但我想应该是两倍,也就是说断言是on的;断言在两种情况下都是on的
发布于 2014-03-06 18:23:36
我们要去Java语言规范
如果值为true,则不采取任何进一步的操作,而assert语句将正常完成。 如果值为false,则执行行为取决于是否存在Expression2:
- If the evaluation completes abruptly for some reason, the assert statement completes abruptly for the same reason.
- If the evaluation completes normally, an AssertionError instance whose "detail message" is the resulting value of Expression2 is created.
- If the instance creation completes abruptly for some reason, the assert statement completes abruptly for the same reason.
- If the instance creation completes normally, the assert statement completes abruptly by throwing the newly created AssertionError object.
如果禁用断言,则跳过assert,assertsOn为true,因此执行if块。
如果启用断言,则执行assert,assertsOn是true,因此断言传递。assertsOn是true,因此执行if块。
答案是你所说的。
assert is on ; assert is on也许有些东西你没有告诉我们/给我们看,或者说,这里提出的答案缺少正确的答案。
https://stackoverflow.com/questions/22232232
复制相似问题