---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------
反射是我以前从来没接触过的新知识,在视频讲解中称为高新技术。乍一接触感觉很难,真正入门后感觉很有趣味,但是有的时候,仍然感觉不很适应这种用法。这是我学到的集中基本用法:
反射基础:
String str1 = "abc";
Class cls1 = str1.getClass();
Class cls2 = String.class();
Class cls3 = Class.forName("java.lang.String");
反射构造方法:
Constructor constructor1 = String.class.getConstructor(StringBuffer.class);
反射成员变量:
ReflectPoint pt1 = new ReflectPoint(3,5);
Field fieldY = pt1.getClass().getField("y");
System.out.println(fieldY.get(pt1));
数组的反射应用:
printObject(a4);
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);
}
}
HashSet_ArrayList:
Collection collections = new HashSet();//new ArrayList()
ReflectPoint pt1 = new ReflectPoint(3,3);
ReflectPoint pt2 = new ReflectPoint(3,3);
collections.add(pt1);
collections.add(pt2);
collections.remove(pt1);
System.out.println(collections.size());
反射技术开发框架:
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);
类加载器:
InputStream ips = ReflectTest2.class.getClassLoader().getRecourceAsStream("cn/itcast/day1/config.properties");
InputStream ips = ReflectTest2.class.getRecourceAsStream("config.properties");
InputStream ips = ReflectTest2.class.getRecourceAsStream("cn/itcast/day1/config.properties");
通过认真地学习反射的基本技术,我感觉我对框架有了更深的认识,为以后的框架学习打下了良好的基础。
---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------
详细请查看:<a href="http://edu.csdn.net/heima" target="blank">http://edu.csdn.net/heima</a>