反射操作は、共有するのに便利です.

1844 ワード

package dao;

public class Users {
	private String name;
	public Users(){
		
	}
	public Users(String xx){
		System.out.println(" "+xx) ;
	}
	public void vv(String xx){
		System.out.println(xx+"===2010====") ;
	}

}

 package dao;
import java.lang.reflect.*;

public class Test {
	// 
	static public void field(){
		try{
	      Class  clazz=Class.forName("dao.Users") ;
	      Class cl[]=new Class[]{String.class,int.class} ;
	      Object object[]=new Object[]{" ",2008} ;
	      Constructor counstr= clazz.getDeclaredConstructor(cl) ;
	      Object obj=counstr.newInstance(object) ;
		  Field[] fie=clazz.getDeclaredFields() ;
		  for(Field f:fie){
			  f.setAccessible(true) ;
			  f.set(obj," ");
		  System.out.println(f.getName()+f.get(obj)) ;
		  System.out.println(f.getModifiers());
		     f.setAccessible(false) ;
		    
		  }
		}catch(Exception e){
			e.printStackTrace() ;
		}
	}
	// 
    static public void main(String[] args){
    	try{
	   Class clazz=Class.forName("dao.Users");
	   Object obj=clazz.newInstance() ;
	   
	   Method mdethod[]=clazz.getDeclaredMethods() ;
	   for(Method mm:mdethod){
		   mm.setAccessible(true) ;
		   Object obb[]=new Object[]{" "} ;
		   mm.invoke(obj,obb) ;
		   System.out.println(mm.getName()) ;
		   System.out.println(mm.getModifiers()) ;
		   mm.setAccessible(false) ;
		  
	   }
    	}catch(Exception e){
    		e.printStackTrace() ;
    	}
    }

}