反射+泛型
import java.lang.reflect.Field; public class Hac<T> { public static void main(String[] args) throws Exception{ Student student = new Student(); new Hac<Student>().writeRecord(student); } public synchronized void writeRecord(T t) throws Exception { Class classzz = t.getClass(); Field[] fieldArray = classzz.getDeclaredFields(); for (Field f : fieldArray) { //获取每个属性名,对应表中的每个字段 f.setAccessible(true); try { String fieldName = f.getName(); String value = String.valueOf(f.get(t)); System.out.println(fieldName+":"+value); } catch (Exception e) { } } }} class Student{ private String name ="zhang"; private int age=20; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
反射+泛型抽取公用utils
原文地址:https://www.cnblogs.com/the-fool/p/11277954.html
时间: 2024-10-07 22:27:22