把类序列化到文件,可以看这篇帖子
http://topic.csdn.net/u/20080807/00/53a344ab-1a16-4585-a242-6335e9fcdbf5.html
这帖子太乱了,看 12 楼我整理的东西就行了
名为 QQ 的类的代码,可以在顶楼看到至于反射,我觉得掌握以下几个方法就够了(也许是我没接触到高深的东西)
具体怎么用,最好查帮助文档。我写的肯定没帮助详细Class c = test.getClass();// 得到某个类对应的“类”
Method[] methods = c.getMethods();// 得到某个类的所有方法,包括父类非私有方法
Method m = methods[0];
m.getParameterTypes();// 得到该方法参数的返回类型
m.invoke(test, null);// 执行该方法。test是对哪个对象执行,null是参数。如果方法无参数,则为null ;如果有参数,就是一个Object数组
m.getName();// 得到方法名称
m.getReturnType();// 得到方法返回类型
LZ如果要练习反射,可以尝试写一下BeanUtils.copyProperties(Object obj,Object obj)
框架里的一个方法,把第一个实体类的属性,赋给第二个或给定一个实体类的java.util.List集合,通过这个集合生成HTML中的table代码

解决方案 »

  1.   

    忘说了,如果要序列化为XML文件,可以 google 一下 xstream 的使用
      

  2.   

    copyProperties
    框架里的方法比我写得好,那个可以自动转换类型,比如float转换为double
    我写的方法,要求两个属性类型必须严格匹配import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;public class BeanUtils {
    private BeanUtils() { } public static synchronized void copyProperties(Object destination,
    Object source) {
    // 得到两个实体类的类
    Class destinationClass = destination.getClass();
    Class sourceClass = source.getClass(); // 得到两个类的所有方法
    Method[] destinationMethods = destinationClass.getMethods();
    Method[] sourceMethods = sourceClass.getMethods(); for (int i = 0; i < destinationMethods.length; i++) {
    for (int j = 0; j < sourceMethods.length; j++) {
    // 当前正在处理的两个方法
    Method currentDestinationMethod = destinationMethods[i];
    Method currentSourceMethod = sourceMethods[j]; // 如果两个方法长度小于4,则肯定不是get或set方法
    if (currentDestinationMethod.getName().length() < 4
    || currentSourceMethod.getName().length() < 4) {
    continue;
    } // 源类方法名以get开头,目标类方法名以set开头,否则继续
    if (!currentDestinationMethod.getName().startsWith("set")
    || !currentSourceMethod.getName().startsWith("get")) {
    continue;
    } // 方法名称必须对应
    if (!currentDestinationMethod.getName().substring(3).equals(
    currentSourceMethod.getName().substring(3))) {
    continue;
    } // 源类方法参数数量必须为0,否则不是get方法;目标类方法参数数量必须为1,否则不是set方法
    Class[] currentDestinationMethodParameterTypes = currentDestinationMethod
    .getParameterTypes();
    Class[] currentSourceMethodParameterTypes = currentSourceMethod
    .getParameterTypes();
    if (currentDestinationMethodParameterTypes.length != 1
    || currentSourceMethodParameterTypes.length != 0) {
    continue;
    } // 源类方法返回类型必须和目标类方法参数类型一致
    if (!currentDestinationMethodParameterTypes[0]
    .equals(currentSourceMethod.getReturnType())) {
    continue;
    } // 赋值
    try {
    Object parameter = currentSourceMethod.invoke(source, null);
    currentDestinationMethod.invoke(destination,
    new Object[] { parameter });
    } catch (IllegalArgumentException e) {
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    e.printStackTrace();
    }
    }
    }
    } public static void main(String[] args) {
    EntityA a = new EntityA(1, "名称", 123F, "abc");
    EntityB b = new EntityB();
    BeanUtils.copyProperties(b, a);
                    // 前两个属性满足条件,第三个是类型不匹配,最后一个是名称不匹配
    System.out.println(b);
    }
    }class EntityA {
    private int id; private String productName; private float price; private String bbb; public String getBbb() {
    return bbb;
    } public void setBbb(String bbb) {
    this.bbb = bbb;
    } public int getId() {
    return id;
    } public void setId(int id) {
    this.id = id;
    } public float getPrice() {
    return price;
    } public void setPrice(float price) {
    this.price = price;
    } public String getProductName() {
    return productName;
    } public void setProductName(String productName) {
    this.productName = productName;
    } public EntityA() { } public EntityA(int id, String productName, float price, String bbb) {
    this.id = id;
    this.productName = productName;
    this.price = price;
    this.bbb = bbb;
    }
    }class EntityB {
    private int id; private String productName; private double price; private String aaa; public String getAaa() {
    return aaa;
    } public void setAaa(String aaa) {
    this.aaa = aaa;
    } public int getId() {
    return id;
    } public void setId(int id) {
    this.id = id;
    } public double getPrice() {
    return price;
    } public void setPrice(double price) {
    this.price = price;
    } public String getProductName() {
    return productName;
    } public void setProductName(String productName) {
    this.productName = productName;
    } public EntityB() {
    } public EntityB(int id, String productName, double price, String aaa) {
    this.id = id;
    this.productName = productName;
    this.price = price;
    this.aaa = aaa;
    } public String toString() {
    return this.id + "," + this.productName + "," + this.price + ","
    + this.aaa;
    }
    }
      

  3.   

    晕,应该选JAVA,选成HTML代码了