gpt4 book ai didi

java - "T is a top-level class, and an assert statement lexically nested within T is executed."是什么意思?

转载 作者:搜寻专家 更新时间:2023-10-30 20:01:08 24 4
gpt4 key购买 nike

我正在学习“类和接口(interface)的初始化”,它说“T 是顶级类,并且执行词法嵌套在 T 中的断言语句。”任何人都可以告诉我“T 是顶级类,并且执行词法嵌套在 T 中的断言语句”是什么意思。举例说明?

这句话来自JLS ,原文是这样的:

A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

  • T is a class and an instance of T is created.
  • T is a class and a static method declared by T is invoked.
  • A static field declared by T is assigned.
  • A static field declared by T is used and the field is not a constant variable (§4.12.4).
  • T is a top-level class, and an assert statement (§14.10) lexically nested within T is executed.

最佳答案

我可以给出部分解释。它指的是启用/禁用断言。断言由 -ea vm 参数启用。

assert 的一个重点是:

An assert statement that is executed before its class has completed initialization is enabled.

假设未给出 -ea 并且您运行以下代码:

 public class Q1 {
public static void main(String[] args) {
Bar b = new Bar();
}
}
class Bar {
static {
boolean enabled = false;
assert enabled = false; //line(a)
System.out.println("Asserts " +
(enabled ? "enabled" : "disabled"));
System.out.println("as");
Baz.testAsserts();
}
}
class Baz extends Bar {
static void testAsserts() {
boolean enabled = false;
assert enabled = false;
System.out.println("Asserts " +
(enabled ? "enabled" : "disabled"));
}
}

在上面的例子中,当 b 被初始化时,Java 保证在 line(a) 被调用之前,断言被禁用(即 line(a) 不被执行根本)。因为断言启用/禁用是类初始化的一部分,因此在您显示的相关语句中提到了它。

之所以提到顶级类(class)而不是其他所有类(class),原因在于此 。更详细的行为在这里:

public class Q1 {
public static void main(String[] args) {
Baz.testAsserts();
// Will execute after Baz is initialized.
}
}
class Bar {
static {
Baz.testAsserts();
// Will execute before Baz is initialized!
}
}
class Baz extends Bar {
static void testAsserts() {
boolean enabled = false;
assert enabled = false;
System.out.println("Asserts " +
(enabled ? "enabled" : "disabled"));
}
}

即使未使用 -ea 标志,它仍然会抛出 AssertionException。这是发生的事情:

  1. Q1.main 被调用
  2. Q1.main 调用 Baz.testAsserts。
  3. 因为 Baz 扩展了 Bar 而 Bar 没有被初始化,根据 JLS 它尝试初始化 Bar
  4. Bar 的静态 block 被调用。请记住,在其类完成初始化或调用 assert 之前启用 assert 语句(以先发生者为准)。在这种情况下,在这个阶段 true 因为 Bar 还没有完全初始化
  5. Bar 的静态调用 Baz.testAsserts()。断言仍处于启用状态(请记住,禁用断言与类初始化有关,而 Bar 仍未完全初始化)。现在 Baz.testAsserts() 抛出 AssertionException

上面是一个环孔。 JLS 只保证在顶级类中执行任何 assert 之前,它将禁用/启用(无论给定什么 vm 参数)它。但如果它不是顶级类,则行为取决于顶级类的初始化。要解释这一点,请参阅:

class Bar {
static {
//Baz.testAsserts();
boolean enabled = false;
assert enabled = false;
System.out.println("Asserts " +
(enabled ? "enabled" : "disabled"));
// Will execute before Baz is initialized!
}
}
class Baz extends Bar {
static void testAsserts() {
boolean enabled = false;
assert enabled = false;
System.out.println("Asserts " +
(enabled ? "enabled" : "disabled"));
}
}

这会打印 Asserts disabled Asserts disabled,因为 Bar 已正确初始化。 Bar 初始化禁用类的 assert,因此 Baz

关于java - "T is a top-level class, and an assert statement lexically nested within T is executed."是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17800829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com