反射,类加载器

来源:互联网 发布:java获取每月第一天 编辑:程序博客网 时间:2024/05/19 12:17
package com.hbsi.reflect;


import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;




public class ReflectTest2 {


/**
* @param args
*/
public static void main(String[] args) throws Exception {
/**getRealPath();//金山词霸/内部
* 一定要记住用完整的路径,但完整的路径不是硬编码,而是运算出来的
*/
//InputStream ips = new FileInputStream("com/hbsi/reflect/config.properties");

InputStream ips = ReflectTest2.class.getClassLoader().getResourceAsStream("com/hbsi/reflect/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 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;
//collections.remove(pt1);

System.out.println(collections.size());
}


}