gpt4 book ai didi

Java - "class"方法/字段来自哪里?

转载 作者:行者123 更新时间:2023-11-30 02:27:05 27 4
gpt4 key购买 nike

“类”方法/字段从何而来?我在 API 中没有找到任何相关信息。

代码示例:

public class SomeClass{
public static void main(String[] args){

System.out.println(SomeClass.class);
}
}

输出:类SomeClass

最佳答案

这是“编译器魔法”,称为类文字:

A class literal is an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type void, followed by a '.' and the token class (JLS 15.8.2)

换句话说,虽然类没有名为 class 的静态成员,但编译器的行为就像它们有一样,根据需要插入对相应 Class 对象的引用。

关于Java - "class"方法/字段来自哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45374762/

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