从Java中的对象获取类
可以使用java.lang.Object.getClass()获得对象的运行时类。同样,该getName()方法用于获取类的名称。
给出一个演示使用该getClass()方法获取对象类的程序,如下所示:
示例
public class Demo {
public static void main (String [] args) {
Integer integer = new Integer(10);
Class c = integer.getClass();
System.out.println ("The class is: " + c.getName());
c = new Demo().getClass();
System.out.println ("The class is: " + c.getName());
}
}输出结果
The class is: java.lang.Integer The class is: Demo
现在让我们了解上面的程序。
首先创建一个新的整数。然后,该getClass()方法用于获取运行时类。最后,该getName()方法用于获取类的名称,即java.lang.Integer并进行打印。演示这的代码片段如下-
Integer integer = new Integer(10);
Class c = integer.getClass();
System.out.println ("The class is: " + c.getName());此后,该getClass()方法用于获取Demo对象的运行时类。然后,该getName()方法用于获取类的名称(即Demo)并打印出来。演示这的代码片段如下-
c = new Demo().getClass();
System.out.println ("The class is: " + c.getName());热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿