java怎么通过字段去获取对象,通过java反射获取任意对象的字段名及字段值

import java.lang.reflect.Field;

/**

*

* @author EX-QINCIDONG001

*

*/

public class ReflectClass {

/**

* @param args

*/

public static void main(String[] args) {

Person p = new Person();

p.setId(0);

p.setName("张三");

reflect(p);

}

public static void reflect(Object obj) {

if (obj == null) return;

Field[] fields = obj.getClass().getDeclaredFields();

for (int j = 0; j < fields.length; j++) {

fields[j].setAccessible(true);

// 字段名

System.out.print(fields[j].getName() + ",");

// 字段值

if (fields[j].getType().getName().equals(

java.lang.String.class.getName())) {

// String type

try {

System.out.print(fields[j].get(obj));

} catch (IllegalArgumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} else if (fields[j].getType().getName().equals(

java.lang.Integer.class.getName())

|| fields[j].getType().getName().equals("int")) {

// Integer type

try {

System.out.println(fields[j].getInt(obj));

} catch (IllegalArgumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

// 其他类型。。。

}

System.out.println();

}

}

class Person {

private int id;

private String name;

public int getId() {

return this.id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return this.name;

}

public void setName(String name) {

this.name = name;

}

}

结果:

id,0 name,张三

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用反射机制来实现依据字段名将一个类A转换为类B的操作。反射机制允许我们在运行时动态地获取类的信息,并且可以通过字段名获取和设置字段。 下面是一个示例代码,演示了如何使用反射实现将类A转换为类B的操作: ```java import java.lang.reflect.Field; public class ReflectionExample { public static <T, U> U convertClass(T source, Class<U> targetClass) throws Exception { U target = targetClass.getDeclaredConstructor().newInstance(); Field[] sourceFields = source.getClass().getDeclaredFields(); Field[] targetFields = targetClass.getDeclaredFields(); for (Field sourceField : sourceFields) { for (Field targetField : targetFields) { if (sourceField.getName().equals(targetField.getName())) { sourceField.setAccessible(true); targetField.setAccessible(true); targetField.set(target, sourceField.get(source)); break; } } } return target; } public static void main(String[] args) throws Exception { // 定义类A和类B class A { private String name; private int age; public A(String name, int age) { this.name = name; this.age = age; } } class B { private String name; private int age; public B() {} 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; } } // 创建类A的实例 A a = new A("John", 25); // 将类A转换为类B B b = convertClass(a, B.class); // 输出转换后的结果 System.out.println("Name: " + b.getName()); System.out.println("Age: " + b.getAge()); } } ``` 上述代码中,`convertClass`方法使用了泛型,可以接受任意类型的源对象和目标类。它通过反射获取对象和目标类的字段,并根据字段名进行匹配和赋操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值