`

java反射

    博客分类:
  • Java
 
阅读更多
java反射

反射机制就是可以把一个类,类的成员(函数,属性),当成一个对象来操作,希望读者能理解,也就是说,类,类的成员,我们在运行的时候还可以动态地去操作他们.

package org;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

public class TestReflection {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
       
        Class ownerClass = Class.forName("org.test");
        Object class1 = ownerClass.newInstance();
       
        Field field = ownerClass.getField("testNumber"); // get public variable in class
        Object propertyObject = field.get(class1);
        System.out.println(propertyObject);
        Method method = ownerClass.getMethod("getNowDateShort", null);
        method.invoke(ownerClass, null);

        String currentDate = new Date().toString();
        Method method2 = ownerClass.getMethod("strToDate2", String.class);
        method2.invoke(ownerClass, currentDate);
    }
}


package org;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class test implements Runnable {
   
    public int testNumber=2;
     private int number;
     public void Calculator(int number) {
         this.number = number;
     }
   
     @Override
     public void run() {
         for (int i=0; i<10; i++) {
             System.out.printf("%s: %d*%d = %d\n", Thread.currentThread().getName(), number, i, i*number);
         }
     }
   
     public static void main(String[] args) throws ParseException {    
          Thread threads[] = new Thread[10];      
          for (int i=0; i<10; i++) {
              test calculator = new test();
              calculator.Calculator(3);
              threads[i] = new Thread(calculator);
              if (i==2) {
                threads[i].setPriority(10);
            } else {
                threads[i].setPriority(1);
            }
              threads[i].setName("Thread "+i);
            threads[i].start();
             
          }    
   }
    
   public static String strToDate2(String strDate) throws ParseException { 
         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
         String strtodate =strDate+"  AAAAAAAAA ";
         System.out.println(strtodate);
         return strtodate; 
   }
  
   public static Date strToDate(String strDate) { 
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
    ParsePosition pos = new ParsePosition(0); 
    Date strtodate = formatter.parse(strDate, pos); 
    System.out.println("@@@@ "+String.valueOf(strtodate));
    return strtodate; 
   }
  
   public static Date getNowDateShort() {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
   String dateString = formatter.format(currentTime);
   ParsePosition pos = new ParsePosition(0);
   Date currentTime_2 = formatter.parse(dateString, pos);
   System.out.println("@@@@ "+String.valueOf(currentTime_2));
   return currentTime_2;
   }
  
}

输出结果:
2
@@@@ Thu Dec 24 00:00:00 CST 2015
Thu Dec 24 17:38:33 CST 2015  AAAAAAAAA


1. http://blog.csdn.net/ljphhj/article/details/12858767
2. http://ifeve.com/java-reflection-tutorial/
3. http://www.importnew.com/9078.html
4. http://www.wangyuxiong.com/archives/52040
5. http://azrael6619.iteye.com/blog/429797 ***



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics