一、类的原始信息
public class UserInfo { private long id; private String username; private String password; public UserInfo() { System.out.println("public UserInfo()"); } public UserInfo(long id) { super(); this.id = id; } public UserInfo(long id, String username) { super(); this.id = id; this.username = username; } public UserInfo(long id, String username, String password) { super(); this.id = id; this.username = username; this.password = password; System.out.println("public UserInfo(long id, String username, String password)"); System.out.println(id + " " + username + " " + password); } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public void test() { System.out.println("public void test1()"); } public void test(String address) { System.out.println("public void test2(String address) address=" + address); } public String test(int age) { System.out.println("public String test3(int age) age=" + age); return "我是返回值"; } }
二、获取类的Class对象的四种方式
public static void main(String[] args) throws IOException, ClassNotFoundException { //通过类获取Class对象 Class<UserInfo> aClass1 = UserInfo.class; //通过对象获取Class对象 Class<? extends UserInfo> aClass2 = new UserInfo().getClass(); //通过类加载器加载全路径获取Class对象 Class<?> aClass3 = UserInfo.class.getClassLoader().loadClass("org.example.entity.UserInfo"); //通过静态方法获取类的Class对象 Class<?> aClass4 = Class.forName("org.example.entity.UserInfo"); System.out.println(aClass1.hashCode()); System.out.println(aClass2.hashCode()); System.out.println(aClass3.hashCode()); System.out.println(aClass4.hashCode()); }
public UserInfo() 968514068 968514068 968514068 968514068
获取的Class对象hashCode都是一样的,Class对象是单例。