首先定义一个配置文件config.properties
className=java.util.ArrayList
然后
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.util.Collection;
- import java.util.Properties;
- public class ReflectTest2 {
- public static void main(String[] args) throws Exception {
- InputStream ips=new FileInputStream("config.properties");
- Properties prop=new Properties();
- prop.load(ips);
- ips.close();
- String className=prop.getProperty("className");
- Collection collections=(Collection)Class.forName(className).newInstance();
- 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());
- }
- }
复制代码 |
|