标签:
反射技术;其实就是动态加载一个指定的类,并获取该类中的所有内容。import java.lang.reflect.*; class most1{ public static void main(String[] args)throws ClassNotFoundException{ String s1="abc"; Class c1=s1.getClass(); Class c2=String.class; Class c3=Class.forName("java.lang.String"); System.out.println(c1==c2); System.out.println(c1==c3); System.out.println(c1.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()); } }反射;就是把java类中的各个成分映射成相应的类,反射比较消耗资源。
import java.lang.reflect.*; class ReflectDemo{ public static void main(String[] args)throws NoSuchMethodException,InstantiationException,IllegalAccessException,InvocationTargetException{ Constructor tts=String.class.getConstructor(StringBuffer.class); String s1=(String)tts.newInstance(new StringBuffer("abc")); System.out.println(s1.charAt(2)); } }获取类的变量:ReflectPoint pt=new ReflectPoint(3,5);
import java.lang.reflect.*; class ReflectPoint{ public int x; private int y; ReflectPoint(int x,int y){ this.x=x; this.y=y; } } class ReflectDemo{ public static void main(String[] args)throws NoSuchFieldException,IllegalAccessException{ ReflectPoint pt=new ReflectPoint(3,3); Field fieldX=pt.getClass().getField("x"); System.out.println(fieldX.get(pt)); Field fieldY=pt.getClass().getDeclaredField("y"); fieldY.setAccessible(true); System.out.println(fieldX.get(pt)); } }获取类的方法;// Class c1=obj.getClass();
import java.lang.reflect.*; class ReflectDemo{ public static void main(String[] args)throws NoSuchMethodException,IllegalAccessException,InvocationTargetException{ String s="abc"; Class c1=s.getClass(); Method method=String.class.getMethod("charAt",int.class); System.out.println(method.invoke(s,1)); System.out.println(method.invoke(s,new Object[]{2})); } }反射的用法;首先需要提供类的Class对象,获得Class对象的三种方式。
import java.lang.reflect.*; class ReflectPoint{ String s1="abll"; String s2="base"; String s3="itcast"; public String toString(){ return s1+"--"+s2+"--"+s3; } } class ReflectDemo{ public static void main(String[] args)throws IllegalAccessException{ ReflectPoint pt=new ReflectPoint(); Field[] fields=pt.getClass().getDeclaredFields(); for(Field field:fields){ if(field.getType()==String.class){ String str=(String)field.get(pt); String value=str.replace('b','a'); field.set(pt,value); } } System.out.println(pt); } }写一个程序;这个程序能够根据用户提供的类名去执行该类中的main方法。
import java.lang.reflect.*; class ReflectPoint{ public static void main(String[] args)throws InvocationTargetException,ClassNotFoundException,NoSuchMethodException,IllegalAccessException{ for(String str: args){ System.out.println(str); } } } class ReflectDemo{ public static void main(String[] args)throws InvocationTargetException,ClassNotFoundException,NoSuchMethodException,IllegalAccessException{ String startName=args[0]; Method mainMethod=Class.forName(startName).getMethod("main",String[].class); mainMethod.invoke(null,(Object)new String[]{"111","222","333"}); } }具有相同维数和元素类型的数组属于同一类型,具有相同Class实例对象。
import java.lang.reflect.*; class ReflectDemo{ public static void main(String[] args){ String[] arr={"x","y","z"}; printObject(arr); printObject("xyz"); } public static void printObject(Object obj){ Class c=obj.getClass(); if(c.isArray()){ int len=Array.getLength(obj); for(int x=0; x<len; x++){ System.out.println(Array.get(obj,x)); } }else{ System.out.println(obj); } } }
package pack; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; class ReflectPoint{ <span style="white-space: pre;"> </span>private int x; <span style="white-space: pre;"> </span>private int y; <span style="white-space: pre;"> </span>ReflectPoint(int x,int y){ <span style="white-space: pre;"> </span>this.x=x; <span style="white-space: pre;"> </span>this.y=y; <span style="white-space: pre;"> </span>} <span style="white-space: pre;"> </span>public boolean equals(Object obj){ <span style="white-space: pre;"> </span>if(!(obj instanceof ReflectPoint)) <span style="white-space: pre;"> </span>throw new RuntimeException("类型不匹配"); <span style="white-space: pre;"> </span>ReflectPoint r=(ReflectPoint)obj; <span style="white-space: pre;"> </span>return this.x==(r.x)&&this.y==r.y; <span style="white-space: pre;"> </span>} } class ReflectDemo{ <span style="white-space: pre;"> </span>public static void main(String[] args){ <span style="white-space: pre;"> </span>Collection coll=new ArrayList(); <span style="white-space: pre;"> </span>ReflectPoint pt1=new ReflectPoint(3,3); <span style="white-space: pre;"> </span>ReflectPoint pt2=new ReflectPoint(5,5); <span style="white-space: pre;"> </span>ReflectPoint pt3=new ReflectPoint(3,3); <span style="white-space: pre;"> </span>coll.add(pt1); <span style="white-space: pre;"> </span>coll.add(pt2); <span style="white-space: pre;"> </span>coll.add(pt3); <span style="white-space: pre;"> </span>coll.add(pt1); <span style="white-space: pre;"> </span>System.out.println(coll.size()); <span style="white-space: pre;"> </span>} }
标签:
原文地址:http://blog.csdn.net/qiang3570/article/details/44489517