---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
反射就是把Java类中的各种成分映射成相应的java类。例如,一个java类中用一个Class
类的对象表示,一个类中的组成部分:成员变量,方法,构造函数,包等等信息也用一个个的java类来表示,就像汽车是一个类,汽车中的发动机,变速箱等等也是一个个的类。
表示java类的Class类显然要提供一系列的方法,来获取其中的变量,方法,构造方法,修
饰符,包等信息,这些信息就是用相应类的实例对象来表示,它们是Field,Method,Contructor
,Package等等。
class
Class--->代表一类什么样的事务
反射的基石-class
Java类用于描述一类事物的共性,该类实物有什么属性,没有什么属性,至于这个属性的值是什么,则是由这个类的实例对象来确定的,不同的实例对象有不同的属性。Java程序中各个Java类属于同一类事物,描述这类事物的Java类名就是Class。
Class cls1 = Date.class//字节码
Class cls2 = Person.class//字节码
字节码是Class的对象。
p1.getClass();
Class.forName("java.lang.String")//返回字节码,有两种方式:1,这份字节码已经加载过了,已经存在内存中,直接取出,2,java虚拟机中还没有字节码,于是用类加载器加载进来,然后在内存中缓存。
加载字节码的方式:
1,类名.class 例如:System.class
2,对象.getClass() 例如:new Date().getClass()
3, Class.forName("类名") 例如:Class.forName("java.util.Date");//做反射式主要用这个
八个基本类型:boolean,byte,char,short,int,long,float,double;
Class.isPrimitary
数组类型的Class实例对象
Class.isArray();
总之,只要是在源程序中出现的类型,都有各自的Class实例对象,例如:int[],void
System.exit
System.getProperties()
Method --> methodObj1
Constructor类代表某个类中的一个构造方法
得到某个类所有的构造方法:
例子:Constructor[] constructor=Class.forName("java.lang.String").getConstructors();
得到某一个构造方法:
例子:Constructor constructor=Class.forName("java.lang.String").getConstructor(StringBuffer.class);
创建实例对象:
通常方式:String str = new String(new StringBuffer("abc"));
反射方式: String str = (String)constructor.newInstance(new StringBuffer("abc"))
//调用获得的方法时要用到上面相同类型的实例对象。
解析:main(String[] args):
为什么这样写,因为传进来的参数不一定是几个,所有用一个String数组参数。
Field类:
代表某个类中的一个成员变量
注:alt+shift+s 快捷键产生构造方法
反射例子:
ReflectPoint.class
package reflection;
public class ReflectPoint {
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 String toString(){
return str1 + ":" +str2 + ":" +str3;
}
}
package reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
public class reflectDemo {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
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(cls2 == 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));
//jdk1.5新特性:可变参数
ReflectPoint pt1 = new ReflectPoint(3,5);
Field fieldY =pt1.getClass().getField("y");
//fieldY的值是多少?是5?,错!
System.out.println(fieldY.get(pt1));
Field fieldX = pt1.getClass().getDeclaredField("x");//x为私有的
fieldX.setAccessible(true);//抢
System.out.println(fieldX.get(pt1));
changeStringValue(pt1);
System.out.println(pt1);
}
private static void changeStringValue(Object obj) throws Exception{
// TODO 扫描这个类String类型变量的所有
Field[] fields = obj.getClass().getFields();
for(Field field:fields){
//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);
}
}
}
}
小结:反射可以改变一个给定对象的原数据;面向对象的设计(圆——>画圆;门——>开门;方法——>调用)
例:field.getType() 与 String.class 且field的类型是String类型,那么判断他俩是否相等用“==”而不用equals,因为字节码只有一个。用“==”显得自己有水平,面试的时候记得加注释哟,免得面试官不懂!
Method类
Method methodCharAt = String.class.getMethod("charAt",int.class);
System.out.println(methodCharAt.invoke(str1,1));
调用方法:
通常方式:System.out.println(str.charAt(1));
反射方式:System.out.println(charAt.invoke(str1,));
例:写一个程序,这个程序能够根据用户提供的类名,去执行该类中的main方法。为什么要用反射调用呢?
Method methodCharAt = String.class.getMethod("CharAt", int.class);
System.out.println(methodCharAt.invoke(str1, 1));
System.out.println(methodCharAt.invoke(str1, new Object[]{2}));
//TestArguments.main(new String[]{"111","222","333"});
String startingClassName =args[0];
Method mainMethod = Class.forName(startingClassName).getMethod("main", String[].class);
数组的反射:
int[] a2 = new int[4]; //参数个数相同,类型相同。字节码就相同;
int[][] a3 = new int[2][3];
System.out.println(a1.getClass() == a2.getClass());//true
System.out.println(a1.getClass() == a3.getClass());//false
Object aObj1 = a1;
Object aObj2 = a4;
System.out.println(a1);
System.out.println(a4);
System.out.println(Arrays.asList(a1));
System.out.println(Arrays.asList(a4));
Object obj = null;
printObject(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));
}
}
}
ArrayList_HashSet的比较:
HashSet放之前先看内存中是否存在。
Hashcode分析:
反射的作为——>实现框架功能,框架与工具类有区别,工具类被用户的类调用,而框架则是调用用户提供的类。
框架配置文件小例:
/*
* ArrayList_HashSet的比较及Hashcode分析
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Properties;
import java.io.*;
public class ReflectTest {
public static void main(String[] args) throws Exception{
InputStream ips = new FileInputStream("config.properties");
Properties props = new Properties();
props.load(ips);
ips.close();
String className = props.getProperty("className");
Collection collections =(Collection) Class.forName(className).newInstance();
//Collection collections = new ArrayList();
//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);
System.out.println(collections.size());
}
}
配置文件config.properties:
className=java.util.ArrayList
用类加载器的方式管理资源和配置文件:
一定要记住用完整的路径,但完整的路径不是硬编码,而是运算出来的。
InputStream ips = new FileInputStream("config.properties") //可读可写
InputStream ips = ReflectTest2.class.getClassLoader().getResourceAsStream("name")
InputStream ips = ReflectTest2.class.getResourceAsStream(name)//不需要写上目录名
---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net