一、基础类
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 "我是返回值"; } }
二、获取属性、构造方法、方法
public static void main(String[] args) throws IOException, ClassNotFoundException { //通过类获取Class对象 Class<UserInfo> userInfoClass = UserInfo.class; System.out.println("属性列表:"); // 获取属性列表 Field[] declaredFields = userInfoClass.getDeclaredFields(); for (Field declaredField : declaredFields) { System.out.println(declaredField.getName()); } System.out.println("构造方法:"); // 获取构造方法 Constructor<?>[] constructors = userInfoClass.getConstructors(); for (Constructor<?> constructor : constructors) { Class<?>[] parameterTypes = constructor.getParameterTypes(); System.out.println(constructor.getName()+"参数数量:"+ parameterTypes.length+"参数类型:"+ Arrays.toString(parameterTypes)); } System.out.println("方法:"); // 获取方法 Method[] declaredMethods = userInfoClass.getDeclaredMethods(); for (Method declaredMethod : declaredMethods) { System.out.println(declaredMethod); } }
属性列表: id username password 构造方法: org.example.entity.UserInfo参数数量:3参数类型:[long, class java.lang.String, class java.lang.String] org.example.entity.UserInfo参数数量:2参数类型:[long, class java.lang.String] org.example.entity.UserInfo参数数量:1参数类型:[long] org.example.entity.UserInfo参数数量:0参数类型:[] 方法: public void org.example.entity.UserInfo.test() public java.lang.String org.example.entity.UserInfo.test(int) public void org.example.entity.UserInfo.test(java.lang.String) public long org.example.entity.UserInfo.getId() public void org.example.entity.UserInfo.setPassword(java.lang.String) public java.lang.String org.example.entity.UserInfo.getUsername() public void org.example.entity.UserInfo.setId(long) public void org.example.entity.UserInfo.setUsername(java.lang.String) public java.lang.String org.example.entity.UserInfo.getPassword()