读书人

框架的概念及用反照技术开发框架的原理

发布时间: 2012-10-30 16:13:36 作者: rapoo

框架的概念及用反射技术开发框架的原理

import java.io.FileInputStream;import java.io.InputStream;import java.util.*; public class ReflectTest2 { public static void main(String[] args) throws Exception { /*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");//这里使用的是绝对路径,在classpath里 Properties props = new Properties(); props.load(ips); ips.close(); String className = props.getProperty("className"); Collection collections = (Collection)Class.forName(className).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; //y属性参与了哈希码的运算,这里修改了y值,下面的代码无法将无法移除 collections.remove(pt1); */ System.out.println(collections.size()); }}

读书人网 >编程

热点推荐