学习打卡--反射(1)

今天看了一整天的博客,终于看懂了反射,但一些很细节的地方还是有点难理解,可能自己脑袋真的很笨吧。
反射说白了就是通过Class类、Constructor(构造器)类、Field类、Method类来分别获取某个类的类、接口和父类、构造方法、成员属性以及成员方法的一些信息等等。
注意:Class类和普通的class不同,Class类是实际存在的一个类,它的实例对象就是一个类。

以下附上今日的代码:

package com.sy.reflection;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class A extends Object implements ActionListener{
       private int a = 3;
       public Integer b = new Integer(4);
       public A() {}
       public A(int id,String name) {}
       public int abc(int id,String name) {return 0;}
       public void actionPerformed(ActionEvent e) {}
}
package com.sy.reflection;
import java.lang.reflect.*;
public class B {
      public static void main(String[] args) {
          A r = new A();
          Class temp = r.getClass();
          try {
              System.out.println("反射类中所有公有的属性");
              Field[] fb = temp.getFields();
              for(int j = 0;j<fb.length;j++) {
                  Class cl = fb[j].getType();
                  System.out.println("fb:"+cl);
              }
              System.out.println("反射类中所有的属性");
              Field[] fa = temp.getDeclaredFields();
              for(int j = 0;j<fa.length;j++) {
                  Class cl = fa[j].getType();
                  System.out.println("fa:"+cl);
              }
              System.out.println("反射类中所有私有的属性");
              Field fc = temp.getDeclaredField("a");
              fc.setAccessible(true);
              Integer i = (Integer)fc.get(r);
              System.out.println(i);
          }catch(Exception e){
              e.printStackTrace();
          }
      }
    
}
package com.sy.reflection;
import java.io.*;
import java.lang.reflect.*;
public class sampleInterface {
        public static void main(String[] args)throws Exception {
            A raf = new A();
            printInterfaceName(raf);
        }
        public static void printInterfaceName(Object o) {
            Class c = o.getClass();
            Class[] theInterfaces = c.getInterfaces();
            for(int i = 0;i<theInterfaces.length;i++) 
                System.out.println(theInterfaces[i].getName());
                Class theSuperclass = c.getSuperclass();
                System.out.println(theSuperclass.getName());
        }
}
package com.sy.reflection;
import java.lang.reflect.*;
public class sampleConstructor {
    public static void main(String[] args) {
        A r = new A();
        printConstructors(r);
   } 
    public static void printConstructors(A r) {
           Class c = r.getClass();
           String className = c.getName();
           try {
               //用getConstructors()方法获取了反射类的构造方法的集合
               Constructor[] theConstructors = c.getConstructors();
              
               for(int i = 0;i<theConstructors.length;i++) {
                   //用Constructor类的getParameterTypes()获取该构造方法的参数类型数组
                   Class[] parameterTypes = theConstructors[i].getParameterTypes();
                   System.out.print(className+"(");
                   
                   for(int j = 0;j<parameterTypes.length;j++) 
                       System.out.println(parameterTypes[j].getName()+" ");
                   
                       System.out.println(")");
               }
           }catch(Exception e) {
               e.printStackTrace();
           }
    }   
}
package com.sy.reflection;
import java.lang.reflect.*;
public class sampleMethod {
    public static void main(String[] args) {  
    A p = new A();  
    printMethods(p);  
    }  
      
    public static void printMethods(Object o) {  //向上转型
    Class c = o.getClass();  
    String className = c.getName();  
    Method[] m = c.getMethods();//返回方法数组
    
    for(int i=0; i<m.length; i++) {  
    System.out.print(m[i].getReturnType().getName());//打印出返回值类型名称
    System.out.print(" "+m[i].getName()+"(");   
    Class[] parameterTypes = m[i].getParameterTypes();//返回参数类型的一个类的数组
    
    for(int j=0; j<parameterTypes.length; j++){  
    System.out.print(parameterTypes[j].getName()); //打印出参数类型的名称
    //如果有多个参数,就打印出","表示间隔
    if(parameterTypes.length>j+1){  
    System.out.print(",");  
        }  
    }  
      
    System.out.println(")");  
         }  
       } 
    }  

猜你喜欢

转载自www.cnblogs.com/susususu/p/10765732.html
今日推荐