当前位置:编程学习 > JAVA >>

java中的反射

package reflect.test;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

public class ReflectTest {

 /**
  * @param args
  */
 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
  String str1 = "abc";
  Class cls1 = str1.getClass();
  Class cls2 = String.class;
  Class cls3 = Class.forName("java.lang.String");
  System.out.println(cls1 == cls2);
  System.out.println(cls1 == cls3);
 
  System.out.println(cls1.isPrimitive());
  System.out.println(int.class.isPrimitive());
  System.out.println(int.class == Integer.class);
  System.out.println(int.class == Integer.TYPE);
  System.out.println(int[].class.isPrimitive());
  System.out.println(int[].class.isArray()); 
 
  //new String(new StringBuffer("abc"));
  Constructor constructor1 = String.class.getConstructor(StringBuffer.class);
  String str2 = (String)constructor1.newInstance(/*"abc"*/new StringBuffer("abc"));
  System.out.println(str2.charAt(2));
 
  ReflectPoint pt1 = new ReflectPoint(3,5);
  Field fieldY = pt1.getClass().getField("y");
  //fieldY的值是多少?是5,错!fieldY不是对象身上的变量,而是类上,要用它去取某个对象上对应的值
  System.out.println(fieldY.get(pt1));
  Field fieldX = pt1.getClass().getDeclaredField("x");
  fieldX.setAccessible(true);
  System.out.println(fieldX.get(pt1));
 
  changeStringValue(pt1);
  System.out.println(pt1);
 
  Method methodCharAt = String.class.getMethod("charAt", int.class);
  System.out.println(methodCharAt.invoke(str1, 1));
  System.out.println(methodCharAt.invoke(str1, new Object[]{0}));
 
  //TestArguments.main(new String[]{"111","222","333"});
  String startingClassName = args[0];
  Method mainMethod = Class.forName(startingClassName).getMethod("main", String[].class);
  //mainMethod.invoke(null, new Object[]{new String[]{"111","222","333"}});
  mainMethod.invoke(null, (Object)new String[]{"111","222","333"});
 
  int [] a1 = new int[]{1,2,3};
  int [] a2 = new int[4];
  int[][] a3 = new int[2][3];
  String [] a4 = new String[]{"a","b","c"};
  System.out.println(a1.getClass() == a2.getClass());
  /*System.out.println(a1.getClass() == a3.getClass());
  System.out.println(a2.getClass() == a3.getClass());*/
  System.out.println(a4.getClass().getName());
  System.out.println(a1.getClass().getName());
  System.out.println(a1.getClass().getSuperclass().getName());
  System.out.println(a4.getClass().getSuperclass().getName());
 
  Object aObj1 = a1;
  Object aObj2 = a4;
  //Object[] aObj3 = a1;
  Object[] aObj4 = a3;
  Object[] aObj5 = a4;
 
  System.out.println(a1);
  System.out.println(a4);
  System.out.println(Arrays.asList(a1));
  System.out.println(Arrays.asList(a4));
 
  printObject(a4);
  printObject(a1);
 
  printObject("xyz");
 }

 private static void printObject(Object obj) {
  Class clazz = obj.getClass();
  if(clazz.isArray()){
   int len = Array.getLength(obj);
   for(int i=0;i<len;i++){
    System.out.println(Array.get(obj, i));
   }
  }else{
   System.out.println(obj);
  }
 
 }
 
 private static void changeStringValue(Object obj) throws Exception {
  Field[] fields = obj.getClass().getFields();
  for(Field field : fields){
   //if(field.getType().equals(String.class)){
   if(field.getType() == String.class){
    String oldValue = (String)field.get(obj);
    String newValue = oldValue.replace('b', 'a');
    field.set(obj, newValue);
   }
  }
 
 }

}


class TestArguments{
 public static void main(String[] args){
  for(String arg : args){
   System.out.println(arg);
  }
 }
}


-----------------------------------

package cn.itcast.day1;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Properties;

public class ReflectTest2 {

 /**
  * @param args
  */
 public static void main(String[] args) throws Exception{
  // TODO Auto-generated method stub
  /*getRealPath();//金山词霸/内部
  一定要记住用完整的路径,但完整的路径不是硬编码,而是运算出来的。*/
  //InputStream ips = new FileInputStream("config.properties");
 
  //InputStream ips = ReflectTest2.class.getClassLoader().getResourceAsStream("cn/itcast/day1/config.properties");
  //InputStream ips = ReflectTest2.class.getResourceAsStream("resources/config.properties");
  InputStream ips = ReflectTest2.class.getResourceAsStream("/cn/itcast/day1/resources/config.properties");

  Properties props = new Properties();
  props.load(ips);
  ips.close();
  String className1 = props.getProperty("className");
  Collection collections = (Collection)Class.forName(className1).newInstance();
 
  //Collection collections = new HashSet();
  ReflectPoint pt1 = new ReflectPoint(3,3);
  ReflectPoint pt2 = new ReflectPoint(5,5);
  ReflectPoint pt3 = new ReflectPoint(3,3);

  collections.add(pt1);
  collections.add(pt2);
  collections.add(pt3);
  collections.add(pt1);
 
  //pt1.y = 7; 
  //collections.remove(pt1);
 
  System.out.println(collections.size());
 }

}


 

----------------------------------------------

package cn.itcast.day1;

import java.util.Date;

public class ReflectPoint {
 private Date birthday = new Date();
 
 private int x;
 public int y;
 public String str1 = "ball";
 public String str2 = "basketball";
 public String str3 = "itcast";
 
 public ReflectPoint(int x, int y) {
  super();
  this.x = x;
  this.y = y;
 }
 
 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + x;
  result = prime * result + y;
  return result;
 }


 @Override
 public boolean equals(Object obj) {
  if (this ==

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,