基本反射了解

 1 package cn.wh;
 2 /**
 3  * java.lang.Class
 4  * @author 王恒
 5  * @time 2016年11月2日 上午10:39:25
 6  */
 7 public class RedlectTest {
 8     public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
 9         Cat cat=new Cat();System.out.println("--------------------------");
10         
11         Class c1=Class.forName("cn.wh.Cat");
12         Class c2=Cat.class;
13         Class c3=cat.getClass();
14         
15         System.out.println(c1.getName());
16         System.out.println(c1.getSimpleName());
17         //返回一个包含某些 Class 对象的数组,这些对象表示属于此 Class 对象所表示的类的成员的所有公共类和接口。
18         System.out.println(c1.getClasses());
19         System.out.println(c1.getPackage());
20         System.out.println(c1.getModifiers());
21         System.out.println(c1.getInterfaces());
22         System.out.println("接口数量 "+c1.getInterfaces().length);
23         System.out.println(c1.getSuperclass().getName());
24         System.out.println("实例化  "+c1.newInstance());
25     }
26 }
RedlectTest
 1 package cn.constructor;
 2 
 3 import java.lang.reflect.Constructor;
 4 import java.lang.reflect.InvocationTargetException;
 5 
 6 public class TestConstructor {
 7 
 8     public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
 9         Class claxx=Class.forName("cn.wh.Cat");
10         //查看有几个构造方法
11         Constructor[] cs=claxx.getConstructors();
12         System.out.println("有public几个构造方法   "+cs.length);
13         Constructor[] cs2=claxx.getDeclaredConstructors();
14         System.out.println("总共有几个构造方法   "+cs2.length);
15         //暴力反射,即没有public修饰的构造方法,使其能够创建构造方法
16         //c.setAccessible(true);
17         //一个参数
18         Constructor c=claxx.getDeclaredConstructor(String.class);
19         System.out.println(c.getName()+"---"+c.getModifiers());
20         //两个参数
21         Constructor c2=claxx.getDeclaredConstructor(String.class,String.class);
22         System.out.println(c.getName()+"---"+c.getModifiers());
23         //实例化
24         Object obj=c.newInstance("");
25         System.out.println(obj+"---"+obj.getClass().getSimpleName());
26         //Class<?>... parameterTypes  表示任意类型的可变参数
27     }
28 }
TestConstructor
 1 package cn.Field;
 2 
 3 import java.lang.reflect.Field;
 4 
 5 import cn.wh.Cat;
 6 
 7 public class TestField {
 8 
 9     public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException {
10         Class claxx=Class.forName("cn.wh.Cat");
11         Cat cat=(Cat) claxx.newInstance();
12         System.out.println("---------------------");
13         //获取声明字段数组
14         Field[] fs=claxx.getDeclaredFields();
15         System.out.println("数组长度为   "+fs.length);
16         
17         for(Field f:fs){ 
18             f.setAccessible(true);
19             System.out.println(f.getName()+"--"+f.getModifiers()+"---"+f.get(cat));
20         }
21         
22         //获取单个声明字段
23         System.out.println("---------------------获取单个声明字段");
24         Field f2=claxx.getDeclaredField("master");
25         f2.setAccessible(true);
26         System.out.println(f2.getName()+"--"+f2.getModifiers()+"---"+f2.get(cat));
27         System.out.println(f2.getName()+"--"+f2.getModifiers()+"---"+f2.get(null));
28         //若声明的字段是static 类型,则可以为f2.get(null),否则一律加对象
29     }
30 }
TestField
 1 package cn.wh;
 2 
 3 public class Cat extends Animal{//,Maoke{
 4     //protected int age=50;
 5     protected String age="我的年龄50";
 6     public static String master="周老师";
 7     
 8     public Cat(){
 9         System.out.println("Cat.Cat(111)");
10     }
11     public Cat(String age,String sname){
12          
13     }
14     
15     Cat(String master,String sname,String age1){
16         
17     };
18     
19     public Cat(String sname){
20         //super(); 写与不写 都存在!  如果写super()必须放在第一行
21         super(sname);
22         this.sname=sname;
23         //super.eat();
24         //System.out.println("Cat.Cat(222)"+super.age);
25         //System.out.println("Cat.Cat(222)"+age);
26     }
27 //    public float walk(String road){
28 //        System.out.println("Cat.walk()");
29 //        return 60f;
30 //    }
31 //    
32      float walk(){
33         System.out.println("Cat.walk()");
34         m1();
35         return 60f;
36     }
37      
38      
39      public void eat(){
40             System.out.println("Cat.eat(111)");
41         }
42      void m1(){
43          
44      }
45     
46 }
Cat
 1 package cn.wh;
 2 
 3 
 4 
 5 public  class Animal {
 6     
 7     protected  String  sname="";
 8     protected  int age=80;
 9     public static String master="杨老师";
10     public Animal(){
11         //System.out.println("Animal.Animal(111)");
12         sname="";
13     }
14     
15     public Animal(String sname){
16         //System.out.println("Animal.Animal(222)");
17     }
18     
19     public  void eat(){
20         System.out.println("Animal.eat(111)");
21     }
22     
23     public void eat(String foodName){
24         System.out.println("Animal.eat(222)"+foodName);
25     }
26     
27      String  eat(String foodName,int num){
28         System.out.println("Animal.eat(333)"+foodName);
29         return "好";
30     }
31     
32     
33     public void sleep(){
34         
35     }
36      float walk(){
37         System.out.println("Animal.walk()");
38         return 30.5f;
39     }
40     
41     
42     
43 }
Animal
 1 package cn.method;
 2 
 3 import java.lang.reflect.InvocationTargetException;
 4 
 5 import java.lang.reflect.Method;
 6 
 7 import cn.wh.Cat;
 8 
 9 public class TestMethod {
10     
11     public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
12         Cat cat=new Cat();
13         
14         Class claxx=Class.forName("cn.wh.Cat");
15         //获取所有方法         包括实例方法,即对象方法    类名.getInstanll();
16         Method[] ms=claxx.getDeclaredMethods();
17         for(Method m:ms){
18             System.out.println(m.getModifiers()+"--"+m.getName()+"--"+m.getParameterCount()+"--"+m.getReturnType());
19         }
20         
21         Method me=claxx.getDeclaredMethod("walk",String.class);
22         me.setAccessible(true);
23         me.invoke(cat,"貓");
24         
25         Method me2=claxx.getDeclaredMethod("eat");
26         me.setAccessible(true);
27         me2.invoke(cat);
28         
29         claxx.getDeclaredMethod("m1").invoke(null);
30     }
31 }
TestMethod
原文地址:https://www.cnblogs.com/1020182600HENG/p/6022589.html