类对象(user.class)

来源:互联网 发布:php生成二维码代码 编辑:程序博客网 时间:2024/06/15 12:31



转载自: http://duben.iteye.com/blog/367218

在学习反射机制时,总结一下获得类对象方式: 
第一种方式:通过类本身来获得对象

Java代码  收藏代码
  1. Class classname =  this .getClass();  

第二种方式:通过子类的实例获取父类对象

Java代码  收藏代码
  1. ClassName cn =  new  ClassName();  
  2. UserClass = cn.getClass();  
  3. Class SubUserClass = UserClass.getSuperclass();   


第三种方式:通过类名加.class获取对象

  
Java代码  收藏代码
  1. Class ForClass = **.**.ClassName. class ;(类在包中的路径加. class )  

第四种方式:通过类名的字符串获取对象

Java代码  收藏代码
  1. Class ForName = Class.forName( "**.**.ClassName" );  


   这种方式在jdbc中常用到。


举例: 1. 通过类名字字符串得到类的实例, 调用方法

[java] view plaincopy
  1. import java.lang.reflect.Constructor;  
  2. import java.lang.reflect.Method;  
  3. import java.lang.reflect.Modifier;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.Date;  
  6. import java.util.GregorianCalendar;  
  7. import java.util.List;  
  8. import jxl.biff.Type;  
  9.   
  10. import org.springframework.context.ApplicationContext;  
  11. import com.dongxin.dxcs.bean.SysCode;  
  12. import com.dongxin.dxcs.common.ApplicationConfig;  
  13. import com.dongxin.dxcs.common.DateUtil;  
  14. import com.dongxin.dxcs.modeleinit.bizlogic.ModeleinitBiz;  
  15. import com.dongxin.dxcs.bean.*;  
  16. public class MainTest1 {  
  17. public MainTest1(){  
  18.    Class[] sl = {String.class};//将要SET的数据类型   和setPrice中的Price类型匹配  
  19.    Class[] fl = {Float.class};  
  20.    Class[] il = {Integer.class};  
  21.    try{  
  22.     Class c =Class.forName("com.dongxin.dxcs.bean.DatStock5");//注册类  
  23.     Object obj = c.newInstance();//获得该类的实体对象,空构造法方法  
  24.     Object[]   parameter1   = {new Float(4.68f)};//设置该类的setCode(String code)方法的参数  
  25.     c.getMethod("setPrice", fl).invoke(obj, parameter1);//调用setCode(String code)方法  
  26.     System.out.println(c.getMethod("getPrice").invoke(obj));//调用getCode()方法,并打印code属性的值  
  27.      
  28.    }catch (Exception e){  
  29.     e.printStackTrace();  
  30.    }  
  31. }  
  32.   
  33. public static void main(String[] args) {  
  34.     
  35.    MainTest1 frame = new MainTest1();  
  36. }  
  37. }  

1:Class cl=A.class;  

                  JVM将使用类A的类装载器, 将类A装入内存(前提是:类A还没有装入内存),不对类A做类的初始化工作.返回类A的Class的对象。

2:Class cl=对象引用o.getClass();

                  返回引用o运行时真正所指的对象(因为:子对象的引用可能会赋给父对象的引用变量中)所属的类的Class的对象 。

3:Class.forName("类名");

                  .装入类A,并做类的初始化

.getClass()是动态的,其余是静态的。

.class和class.forName()只能返回类内field的默认值,getClass可以返回当前对象中field的最新值

Class.forName() 返回的是一个类,.newInstance() 后才创建一个对象,Class.forName()的作用是要求JVM查找并加载指定的类,也就是说JVM会执行该类的

待反射类:

复制代码
package yerasel;public class Person {    private String name = "Alfira";    public void getName() {        System.out.println(name);    }    public void setName(String name, int a) {        this.name = name + a;    }}
复制代码

反射代码:

复制代码
package yerasel;import java.lang.reflect.Method;public class Test {    /**     * @param args     */    public static void main(String[] args) {        show("yerasel.Person");    }    private static void show(String name) {        try {            // JVM将使用类A的类装载器,将类A装入内存(前提是:类A还没有装入内存),不对类A做类的初始化工作            Class classtype3 = Person.class;            // 获得classtype中的方法            Method getMethod3 = classtype3.getMethod("getName", new Class[] {});            Class[] parameterTypes3 = { String.class, int.class };            Method setMethod3 = classtype3                    .getMethod("setName", parameterTypes3);            // 实例化对象,因为这一句才会输出“静态初始化”以及“初始化”            Object obj3 = classtype3.newInstance();            // 通过实例化后的对象调用方法            getMethod3.invoke(obj3); // 获取默认值            setMethod3.invoke(obj3, "Setting new ", 3); // 设置            getMethod3.invoke(obj3); // 获取最新            System.out.println("----------------");            // 返回运行时真正所指的对象            Person p = new Person();            Class classtype = p.getClass();// Class.forName(name);            // 获得classtype中的方法            Method getMethod = classtype.getMethod("getName", new Class[] {});            Class[] parameterTypes = { String.class, int.class };            Method setMethod = classtype.getMethod("setName", parameterTypes);            getMethod.invoke(p);// 获取默认值            setMethod.invoke(p, "Setting new ", 1); // 设置            getMethod.invoke(p);// 获取最新            System.out.println("----------------");            // 装入类,并做类的初始化            Class classtype2 = Class.forName(name);            // 获得classtype中的方法            Method getMethod2 = classtype2.getMethod("getName", new Class[] {});            Class[] parameterTypes2 = { String.class, int.class };            Method setMethod2 = classtype2                    .getMethod("setName", parameterTypes2);            // 实例化对象            Object obj2 = classtype2.newInstance();            // 通过实例化后的对象调用方法            getMethod2.invoke(obj2); // 获取默认值            setMethod2.invoke(obj2, "Setting new ", 2); // 设置            getMethod2.invoke(obj2); // 获取最新            System.out.println("----------------");        } catch (Exception e) {            System.out.println(e);        }    }}
复制代码

0 0