• 学习打卡--反射(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(")");  
    	     }  
    	   } 
    	}  
    
    
  • 相关阅读:
    hdu-1142(记忆化搜索+dij)
    hdu-1140(求距离,精度判断)
    hdu-1131(卡特兰数+大数)
    hdu-1130(卡特兰数+大数乘法,除法模板)
    hdu-1129(模拟题)
    hdu-1128(数学问题,筛数)
    hdu-1124(数学问题,求n!的尾零的个数)
    hdu-1115(计算多边形重心)
    hdu-1121(差分法--数学问题)
    表达式求值(堆栈)
  • 原文地址:https://www.cnblogs.com/susususu/p/10765732.html
Copyright © 2020-2023  润新知