Class.forName("com.dsheng.depart").newInstance()
类的动态实例化
设置这个对象属性的值,不明白你是想达到个什么目的?

解决方案 »

  1.   

    目的是:比如有package com.dsheng.depart
    depart
    {
      private String id;
      private String name;
      .....set 和get 方法 
    };
    有两个属性,我是想根据com.dsheng.depart动态创建depart的一个对象,并设置其属性id=1,name="ddd"
      

  2.   

    try {
    Object obj=Class.forName("com.dsheng.Depart").newInstance();
    Depart depart=(Depart)obj;
    depart.setId(1);
    depart.setName("ddd");

    } catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    感觉没什么意义
      

  3.   

    这样写是没什么意义,我把我的最终目的写出来:
    类:actionForm  客户端传递近来的,保存客户端数据
    类:entity     逻辑层处理需要的实体类,我的目的是将客户段传递进来的actionForm转换为逻辑层的entity,数据操作层在对entity进行操作,
    因此我有
    一个配置文件:convert.xml
    如下,描述了actionForm和entity的属性的对应关系,以便将数据转化到entity的对应属性里
    <beans>
      <actionforms>
         <actionform id="actionFormA" entity="entityA" class="com.dsheng.actionFormA">
    <field name="id"  entityField="id" />
    <field name="name"  entityField="name" />
      </actionforms>
      <entities>
         <entity name="entityA" class="com.dsheng.entityA">
         </entity>
      <entities>
    </beans>通过转化类:convertFactory 实现
     有个方法
    public Object getEntity(String actionName, Object arg1)
    参数  actionName 是对应配置文件的<actionform id,根据id 的到entity的类名,并得到两个类的属性对应关系,并传递数据,最后返回entity 对象
      

  4.   

    补充一下参数arg1就是客户端的actionForm对象