package edu.wtbu;标签:反射,Class,name,对象,age,id,获得,User,public From: https://www.cnblogs.com/123456dh/p/17254332.html
//反射
public class Demo03 extends Object{
public static void main(String[] args) throws ClassNotFoundException {
User user = new User();
Class<? extends User> aClass = user.getClass();//返回指定类名的class对象
System.out.println(aClass);//class edu.wtbu.User
Class<?> c1 = Class.forName("edu.wtbu.User");
System.out.println(c1);//class edu.wtbu.User
Class<?> c2 = Class.forName("edu.wtbu.User");//返回指定类名的class对象
Class<?> c3 = Class.forName("edu.wtbu.User");
Class<?> c4 = Class.forName("edu.wtbu.User");
//一个类在内存中只有一个class对象
//一个类被加载后,类的整个结构都会被封装在class对象中
System.out.println(c2.hashCode());
System.out.println(c3.hashCode());
System.out.println(c4.hashCode());
}
}
//实体类:pojo或者entity表示
class User{
private String name;
private int id;
private int age;
public User() {
}
public User(String name, int id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", id=" + id +
", age=" + age +
'}';
}
}