概要:
1、getCanonicalName() 是获取所传类从java语言规范定义的格式输出。
2、getName() 是返回实体类型名称
3、getSimpleName() 返回从源代码中返回实例的名称。
例子:
public class Log4jTest {class Innr{}private static Logger log = LogManager.getLogger(Log4jTest.class);@Testpublic void log4jTest(){log.info("-----------内部类----------------");Class innr = Innr.class;log.info(innr.getCanonicalName());log.info(innr.getName());log.info(innr.getSimpleName());log.info("-----------匿名类----------------");Class anonymousClass = new Object(){}.getClass();log.info(anonymousClass.getCanonicalName());log.info(anonymousClass.getName());log.info(anonymousClass.getSimpleName());log.info("-----------数组类----------------");Class arrayClass = new Object[1].getClass();log.info(arrayClass.getCanonicalName());log.info(arrayClass.getName());log.info(arrayClass.getSimpleName());}
}
结果:
17:40:36.940 [main] INFO zengqiang.Log4jTest - -----------内部类----------------
17:40:36.944 [main] INFO zengqiang.Log4jTest - zengqiang.Log4jTest.Innr
17:40:36.944 [main] INFO zengqiang.Log4jTest - zengqiang.Log4jTest$Innr
17:40:36.944 [main] INFO zengqiang.Log4jTest - Innr
17:40:36.944 [main] INFO zengqiang.Log4jTest - -----------匿名类----------------
17:40:36.945 [main] INFO zengqiang.Log4jTest - null
17:40:36.945 [main] INFO zengqiang.Log4jTest - zengqiang.Log4jTest$1
17:40:36.946 [main] INFO zengqiang.Log4jTest -
17:40:36.946 [main] INFO zengqiang.Log4jTest - -----------数组类----------------
17:40:36.946 [main] INFO zengqiang.Log4jTest - java.lang.Object[]
17:40:36.946 [main] INFO zengqiang.Log4jTest - [Ljava.lang.Object;
17:40:36.946 [main] INFO zengqiang.Log4jTest - Object[]
————————————————
等
原文链接:https://blog.csdn.net/zq1994520/article/details/78942684