gpt4 book ai didi

java如何.class属性返回类对象

转载 作者:行者123 更新时间:2023-12-02 05:08:22 24 4
gpt4 key购买 nike

/*
* A class for creating object using .class property
*/
class Customer{

}


public class MyApp {

public static void main(String[] args) {

// Can someone explain how internally it return Class object
Class customerClass = Customer.class;

System.out.println("Class : " + customerClass);
}
}

最佳答案

使用javap -c MyApp,你会看到第一个字节码是做什么的:

$ javap -c MyApp
Compiled from "MyApp.java"
public class MyApp {
...
public static void main(java.lang.String[]);
Code:
0: ldc_w #2 // class Customer
...

换句话说,Customer.class 被编译为 ldc(加载常量)字节码指令,它引用了 Customer 类常量池。

关于java如何.class属性返回类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27559803/

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