本帖最后由 思维 于 2014-8-6 17:20 编辑
今天写了一个关于反射的小程序!实现一个简单的框架功能!一直无法编译通过!谁知道哪里出错了啊?
config.properties内容为className=java.util.ArrayList。上代码:- import java.util.*;
- import java.io.*;
- class ReflectPoint{
- private int x;
- public int y;
- public ReflectPoint(int x,int y){
- this.x=x;
- this.y=y;
- }
- }
- class ReflectTest1{
- 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).getInstance();
- //Collection collection=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());
- }
- }
复制代码
|
|