反射的代码

来源:互联网 发布:淘宝客服忙吗 编辑:程序博客网 时间:2024/06/06 02:18
  1. package com.oracle.reflection;<pre name="code" class="html">package com.oracle.reflection;  
  2.   
  3. import java.util.LinkedList;  
  4.   
  5. public class Test3 {  
  6.   
  7.     public static void main(String[] args) throws InstantiationException, IllegalAccessException {  
  8. //  Class<?> c=LinkedList.class;  
  9. //    Class<?>[]cc=c.getInterfaces();  
  10. //    Class<?>ccc=c.getSuperclass();//获得父类  
  11. //    for(Class c1:cc){  
  12. //      System.out.println(c1.getName());  
  13. //        
  14. //    }  
  15.       
  16.         Class<?> c=String[].class;  
  17.         System.out.println(c.getName());  
  18.         if(c.isArray()){  
  19.             System.out.println("这是一个数组");  
  20.             System.out.println("数组中的元素类型是");  
  21.          Class ac=  c.getComponentType();//获得中数组中元素的类型  
  22.          System.out.println(ac.getName());  
  23.       
  24.         }  
  25.           
  26.         Class<?> c1=Person.class;  
  27.         Object o=c1.newInstance();  
  28.         System.out.println(o.toString());  
  29.       
  30.     }  
  31.   
  32. }  
  33. </pre><br>  
  34. <pre name="code" class="html">package com.oracle.reflection;  
  35.   
  36. import java.lang.reflect.Field;  
  37.   
  38. public class Test4 {  
  39.   
  40.     public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {  
  41.         Class c=Person.class;  
  42.         Person p=new Person(10,"张大妈","女");  
  43.         Field f=c.getDeclaredField("name");  
  44.     //  f.setAccessible(true);//这样设置就能访问私有的变量     
  45.         f.set(p, "小龙女");      
  46.     System.out.println(f.get(p));  
  47.     }  
  48.   
  49. }</pre><br>  
  50. <pre name="code" class="html">package com.oracle.reflection;  
  51.   
  52. import java.lang.reflect.Field;  
  53.   
  54. public class TestStudent {  
  55.   
  56.     public static void main(String[] args) {  
  57.     Class c=Student.class;  
  58.     Field[]fs=c.getDeclaredFields();  
  59.     for(Field f:fs){  
  60.         System.out.println(f.getModifiers()+":"+f.getName());  
  61.         //修饰符的编号,都是2的次方  ,两个修饰符的编号可以相加  
  62.     }  
  63.       
  64.       
  65.   
  66.     }  
  67.   
  68. }  
  69. </pre><br>  
  70. public class Person {int personno;String name;String sex;public void eat(){System.out.println(this.name+"正在吃");}public int getPersonno() {return personno;}public void setPersonno(int personno) {this.personno = personno;}public String getName() {return name;}public  
  71.  void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Person(int personno, String name, String sex) {super();this.personno = personno;this.name = name;this.sex = sex;}public  
  72.  Person() {super();}}  
  73. <pre></pre>  
  74. <br>  
  75. <pre name="code" class="html">package com.oracle.reflection;  
  76.   
  77. public class Student extends Person {  
  78. int score;  
  79. public String school;  
  80.       
  81.       
  82.       
  83.       
  84. }  
  85. </pre><br>  
  86. <pre name="code" class="html">package com.oracle.reflection;  
  87.   
  88. import java.lang.reflect.Field;  
  89.   
  90. public class Test {  
  91.   
  92.     public static void main(String[] args) {  
  93.     //1.先获得类实例,这是反射的起点  
  94.     //  Class c=Person.class;  
  95. //      System.out.println(c.getName()) ;//包名和类名  
  96. //      System.out.println(c.getSimpleName());//只是类名  
  97.   
  98.     //当前这个类有多少个属性,每个属性是什么  
  99.         //Field []fs=c.getFields();//java.lang.reflect包  
  100.         //System.out.println(fs.length);//只能获得共有的  
  101.   
  102.           
  103.         //          //方法2  
  104.         Person p=new Person();  
  105.         Class cc=p.getClass();  
  106.           
  107.     Field []fs=cc.getDeclaredFields();  
  108.     for(Field f:fs){  
  109.         System.out.println(f.getName()+","+f.getType());  
  110.         }  
  111.       
  112.           
  113.           
  114.     }  
  115.   
  116. }  
  117. </pre><br>  
  118. <pre name="code" class="html">package com.oracle.reflection;  
  119.   
  120. import java.lang.reflect.Field;  
  121. import java.util.Scanner;  
  122.   
  123. public class Test2 {  
  124.   
  125.     public static void main(String[] args) throws ClassNotFoundException {  
  126. //  Scanner s=new Scanner(System.in);  
  127. //  System.out.println("请输入一个类名");  
  128. //     String name=s.nextLine();  
  129. //       
  130. //    Class c=Class.forName(name) ;  
  131. //System.out.println("这个类是:"+c.getSimpleName());       
  132. //Field []fs=c.getDeclaredFields();  
  133. //for(Field f:fs){  
  134. //  System.out.println(f.getName()+","+f.getType());  
  135. //  }  
  136. //       
  137.         Person p=new Person();  
  138. insert(p);  
  139.     }  
  140. //利用反射机制可以拼接出一个sql语句  
  141.     public static void insert(Object obj){  
  142.         Class c=obj.getClass();  
  143.         String name=c.getSimpleName();//类名就是表名  
  144.         String sql="insert into "+name+"(";  
  145.           
  146.         String value="values(";  
  147.         Field[]fs=c.getDeclaredFields();  
  148.         for(int i=0;i<fs.length;i++){  
  149.             if(i==fs.length-1){  
  150.                 sql+=fs[i].getName()+")";  
  151.                 value+="?)";  
  152.             }else{  
  153.                 sql+=fs[i].getName()+",";  
  154.             value+="?,";  
  155.             }     
  156.         }  
  157.     System.out.println(sql+value);  
  158.     //ps=conn.prepareStatement(sql+value)  
  159.     for(int i=0;i<fs.length;i++){  
  160.         Field f=fs[i];  
  161.         f.setAccessible(true);  
  162.         //ps.setObject(i+1,f.get(obj))  
  163.           
  164.     }  
  165.     //ps.execute();  
  166.       
  167.     }  
  168.       
  169.   
  170.       
  171. }  
  172. </pre>  
  173. <p>上面是类和属性的反射,下面是方法的反射</p><pre name="code" class="html">package fangfa;  
  174.   
  175. public class Person {  
  176. String name;  
  177. String sex;  
  178. public void eat(){  
  179.     System.out.println(this.name+"正在吃");  
  180.       
  181. }  
  182. private void eat(String food){  
  183.     System.out.println(this.name+"正在吃"+food);  
  184.       
  185. }  
  186. protected void drink(){  
  187.     System.out.println(this.name+"正在喝");  
  188. }  
  189. public Person(String name, String sex) {  
  190.     super();  
  191.     this.name = name;  
  192.     this.sex = sex;  
  193. }  
  194. public Person() {  
  195.     super();  
  196. }  
  197. private Person(String name) {  
  198.     super();  
  199.     this.name = name;  
  200. }  
  201.   
  202.   
  203.   
  204.   
  205. }</pre><br>  
  206. <pre name="code" class="html">package fangfa;  
  207.   
  208. import java.lang.reflect.Constructor;  
  209. import java.lang.reflect.InvocationTargetException;  
  210.   
  211. public class TestConstructor {  
  212.   
  213.     /** 
  214.      * 得到构造方法(用的少) 
  215.      * @throws NoSuchMethodException  
  216.      * @throws SecurityException  
  217.      * @throws InvocationTargetException  
  218.      * @throws IllegalAccessException  
  219.      * @throws InstantiationException  
  220.      * @throws IllegalArgumentException  
  221.      */  
  222.     public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {  
  223.         Class c=Person.class;  
  224.     //  Constructor[]cs=c.getDeclaredConstructors();  
  225. //      for(Constructor cc:cs){  
  226. //          System.out.println(cc.getName());  
  227. //          Class[]pc=cc.getParameterTypes();  
  228. //          if(pc.length==0){  
  229. //              System.out.println("无参数");  
  230. //          }else{  
  231. //              for(Class pcc:pc){  
  232. //                  System.out.println(pcc.getName());  
  233. //              }  
  234. //          }  
  235.     //  }  
  236.           
  237.         Constructor cs=c.getDeclaredConstructor(String.class);    
  238.         cs.setAccessible(true);  
  239.         Object o=cs.newInstance("李四");  
  240.         System.out.println(o);  
  241.           
  242.     Object o1=  c.newInstance();  
  243.     System.out.println(o1);  
  244.     }  
  245.   
  246. }</pre><br>  
  247. <pre name="code" class="html">package fangfa;  
  248.   
  249. import java.lang.reflect.Method;  
  250.   
  251. public class TestMethod {  
  252.   
  253.     /** 
  254.      * @param args 
  255.      * @throws NoSuchMethodException  
  256.      * @throws SecurityException  
  257.      */  
  258.     public static void main(String[] args) throws SecurityException, NoSuchMethodException {  
  259.     Class c=Person.class;  
  260.     //Method[]ms=c.getMethods();  
  261. //  Method[]ms=c.getDeclaredMethods();  
  262. //  for(Method m:ms){  
  263. //      System.out.println(m.getName());  
  264. //  }  
  265.       
  266.       
  267. //  Method ms=c.getDeclaredMethod("eat",String.class);  
  268. //    
  269. //  System.out.println(ms.getName());  
  270. //    
  271. //    Class<?>[]ct=ms.getParameterTypes();  
  272. //  for(Class c1:ct){  
  273. //      System.out.println(c1.getName());  
  274. //  }  
  275.       
  276.       
  277.     //每个方法的名,参数,返回值  
  278.       
  279.     Method[]ms=c.getMethods();  
  280.     for(Method m:ms){  
  281.         System.out.println(m.getReturnType().getSimpleName()+","+m.getName());  
  282.     //获得参数  
  283.         Class<?>[]cp=m.getParameterTypes();  
  284.         if(cp.length!=0){  
  285.             for(Class<?> c1:cp){  
  286.                 System.out.println("\t"+ c1.getName());  
  287.                   
  288.             }  
  289.         }else{  
  290.     System.out.println("\t   无参数");  
  291.     }  
  292.       
  293.       
  294.     }}  
  295.     }  
  296.   
  297. </pre><br>  
  298. <p></p>