黑马程序员_java基础反射的类型与应用

来源:互联网 发布:北京集佳待遇知乎 编辑:程序博客网 时间:2024/05/22 05:10
------ <a href="http://www.itheima.com" target="blank">Windows Phone 7手机开发</a>、<a href="http://www.itheima.com" target="blank">.Net培训</a>、期待与您交流! -------
//一个class代表一个字节码,一个method代表字节码中的一个方法。/*Constructor反射类代表字节码中的构造方法*//*File反射类*//*用反射调用对象的方法*///str1.charAt(1);Method methodCharAt = String.class.getMethod("charAt",int.class)System.out.println(methodCharAt.invoke(str1,1));//画圆的方法用到圆心和半径,circle.draw();列出把列车刹住了System.out.println(methodCharAt.invoke(str1,new Object[]{2});//new int[] {1,2,3},jdk1.5有装箱的功能,此处将interager对象2放入数组/*用反射方式执行某个类中的main方法mainMethod.invoke(null.new Object[]{new String[]{"xxx"}});mainMethod.invoke(null.(Object)new String[]{"xxx"});,编译器会作特殊处理,编译时不把参数当数组看待,也就不会将数组打散成若干个参数。*/String startingClassName = args[0];Method mainMethod = Class.forName(StartingClassName).getMethod("main",String);mainMethod.invoke(null, (Object)new String []{"111","222","333"});/*数组的反射*/int[] a1 = new int[3];int[] a2 = new int[4];int[][] a3 = new int[2][3];//数组里装的是ObjectString[] a4 = new String[]{"a","b","c"};System.out.println(a1.getclass()==a2.getclass());//类转换成字节码之后才能比较System.out.println(a1.getclass()==a4.getclass());System.out.println(a1.getclass()==a3.getclass());System.out.println(a1.getClass().getName());System.out.println(al.getClass().getSuperclass().getName());System.out.println(a4.getClass().getSuperclass().getName());Object aObj1 = a1;Object aObj2 = a4;Object[] aObj3 = a1;//基本类型的数组,不可以转换成Object数组Object[] aObj4 = a3;//打印数组 Arrays有对数组进行操作的大量方法System.out.println(Arrays.asList(a4));//字符串的可以打印,按Object进行处理System.out.println(Arrays.asList(a1));//基本类型参数,按照jdk1.5,视作一个参数,不可打印Object obj = null;printObjext(Obj);private static void printObject(Object obj){Class class = obj.getClass();if(class.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);}}//结果,是数组就拆开,不是数组就整个打印。//可以得到数组某个元素的类型,但不能得到整个数组的类型/*反射的作用——*/public static void main(String[] args){Collection collections = new HashSet();//ArrayList()时集合的大小是4ReflectPoint pt1 = new ReflectPoint(3,3);ReflectPoint pt2 = new ReflectPoint(5,5);ReflectPoint pt3 = new ReflectPoint(3,3);    collectiion.add(pt1);collectiion.add(pt2);collectiion.add(pt3);collectiion.add(pt1);pt1.y=7;collextions.remove(pt1);//该了之后hashCode码改变了,导致内存泄露Systtem.out.println(collections.size());//HashSet集合大小为3}//HashCode的作用Object类中定义了一个hashCode()方法来返回每个Java对象的哈希码,当从HashSet集合中查找某个对象时,java首先调用hashCode()方法获得该对象的哈希码,找到相应的存储区域,用equals方法进行比较。当一个对象存储在hashCode集合中,以后就不要修改参与hashCode计算的字段。/*反射实现框架功能框架与工具类有区别,工具类被用户的类调用,而框架则是调用用户提供的类。因为在写程序时,无法知道要调用的类名,所以,在程序中无法直接new某个类的实例对象了,而要用反射方式来做。*/config.properties文件className*java.util.ArrayList//这个类变成HashSet结果变为2。放在配置文件中,源程序中不要出现其明字。public static void main(Sring[] args) throws Exception{InputStream ips = new FileInputStream("config.properties")Properties props = new Properties();//Propeties和hashSet类似,但其中存在的键值在初始化时会被加载props.load(ips);ips.close();//释放资源String className = props.getPropetrty("className");Collection collections = (Collection).Class.forName(className).newInstance();

0 0