日常开发中获取Class对象的五种方式
- 类名.class
- 对象名.getClass()
- Class.forName(类的完全限定名)
- 借助一个类的Class对象 使用加载器的 loadClass(类的完全限定名) 去加载另外一个类
- 包装类的TYPE属性
代码示例
准备两个JavaBean
package com.lihaozhe.bean;
/**
* @author 李昊哲
* @version 1.0.0 2022/7/18 上午9:23
*/
public class Emp {
private String name;
private Integer age;
private Integer gender;
static {
System.out.println("我是静态代码块");
}
{
System.out.println("我是构造代码块");
}
public Emp() {
System.out.println("我是无参构造方法");
}
public Emp(String name, Integer age, Integer gender) {
System.out.println("我是有参构造方法");
this.name = name;
this.age = age;
this.gender = gender;
}
public static void haha() {
System.out.println("我是public static修饰的方法");
System.out.println("仰天长啸");
}
public void heihei() {
System.out.println("我是public修饰的方法");
System.out.println("嘿嘿嘿");
}
private void hehe() {
System.out.println("我是private修饰的方法");
System.out.println("粉面含春威不露,丹唇未起笑先闻");
}
public Integer sum(Integer a, Integer b) {
return a + b;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public int getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
@Override
public String toString() {
return this.getClass().getSimpleName() + "{" +
"name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
'}';
}
}
package com.lihaozhe.bean;
/**
* @author 李昊哲
* @version 1.0.0 2022/7/18 上午9:42
*/
public class Person {
private String nickname;
private int age;
private int gender;
public Person() {
}
public Person(String nickname, int age, int gender) {
this.nickname = nickname;
this.age = age;
this.gender = gender;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
}
编写测试类
package com.lihaozhe.reflect;
import com.lihaozhe.bean.Emp;
import com.lihaozhe.bean.Person;
import org.junit.jupiter.api.Test;
/**
* 加载class文件
* 类名.class
* 对象名.getClass()
* Class.forName(类的完全限定名)
* 借助一个类的Class对象 使用加载器的 loadClass(类的完全限定名) 去加载另外一个类
* 包装类的TYPE属性
* @author 李昊哲
* @version 1.0.0 2022/7/18 上午9:19
*/
public class ClassLoaderTest {
@Test
public void test00() {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
// app
System.out.println(classLoader.getName());
// platform
System.out.println(classLoader.getParent().getName());
// java.lang.NullPointerException: Cannot invoke "java.lang.ClassLoader.getName()" because the return value of "java.lang.ClassLoader.getParent()" is null
System.out.println(classLoader.getParent().getParent().getName());
}
@Test
public void test01() {
// com/lihaozhe/bean/Emp.class
Class<Emp> empClass = Emp.class;
// com.lihaozhe.bean.Emp
System.out.println(empClass.getName());
// Emp
System.out.println(empClass.getSimpleName());
}
@Test
public void test02() {
// com/lihaozhe/bean/Emp.class
Class<? extends Emp> aClass = new Emp().getClass();
// com.lihaozhe.bean.Emp
System.out.println(aClass.getName());
// Emp
System.out.println(aClass.getSimpleName());
}
@Test
public void test03() {
// com/lihaozhe/bean/Emp.class
Class<?> aClass = null;
try {
aClass = Class.forName("com.lihaozhe.bean.Emp");
// com.lihaozhe.bean.Emp
System.out.println(aClass.getName());
// Emp
System.out.println(aClass.getSimpleName());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@Test
public void test04() {
ClassLoader classLoader = Person.class.getClassLoader();
// com/lihaozhe/bean/Emp.class
Class<?> aClass = null;
try {
aClass = classLoader.loadClass("com.lihaozhe.bean.Emp");
// com.lihaozhe.bean.Emp
System.out.println(aClass.getName());
// Emp
System.out.println(aClass.getSimpleName());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@Test
public void test05() {
Class<Integer> type = Integer.TYPE;
// int
System.out.println(type);
// int
System.out.println(type.getName());
// int
System.out.println(type.getSimpleName());
}
}