Java反射 具体需求实现案例三

来源:互联网 发布:mac 百度云下载太慢 编辑:程序博客网 时间:2024/04/30 11:11

import java.io.FileInputStream;import java.io.InputStream;import java.util.Collection;import java.util.Properties;class Point {int x, y;@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + x;result = prime * result + y;return result;}@Overridepublic 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;}}//从配置文件config.properties中获得 程序所用集合的类名public class Test {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 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());}}

配置文件:  当前目录   config.properties

配置文件内容:

className=java.util.ArrayList


0 0
原创粉丝点击