package test;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;
class Point {
int x, y;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
}
//用类加载器
public class Test {
public static void main(String[] args) throws Exception {
//InputStream ips = new FileInputStream("config.properties");
InputStream ips = Point.class.getClassLoader().getResourceAsStream("test/config.properties");
Properties props = new Properties();
props.load(ips);
ips.close();
String className = props.getProperty("className");
Collection collection = (Collection)Class.forName(className).newInstance();
Point pt1 = new Point(1, 1);
Point pt2 = new Point(2, 3);
Point pt3 = new Point(1, 1);
collection.add(pt1);
collection.add(pt2);
collection.add(pt3);
System.out.println(collection.size());
}
}
配置文件: test/config.properties
配置文件内容:
className=java.util.ArrayList