---------------------- android培训、java培训、期待与您交流! ----------------------
---------------------- android培训、java培训、期待与您交流! ----------------------
近期对反射有了一定的认识,通过对张老师讲解的视频,运用反射技术用来做框架,哈哈,下面向大家介绍一下用反射识别一个类,在配置文件中配置那个类就可以源程序中识别。
创建类ReflectPoint
package reflect;
public class ReflectPoint {
private int x;
public int y;
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
}
public class ReflectPoint {
private int x;
public int y;
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
}
创建配置文件config.properties
className=java.util.HashSet
className1=java.util.ArrayList
className1=java.util.ArrayList
然后应用反射对这个配置文件识别
package reflect;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;
public class ReflectDemo2 {
@SuppressWarnings("unchecked")
public static void main(String[] args){
try {
InputStream ips= new FileInputStream("D:/javaworkplace/heima/src/reflect/config.properties");//修改为自己的目录
Properties props=new Properties();
try {
props.load(ips);
} catch (IOException e) {
e.printStackTrace();
}
try {
ips.close();
String className=props.getProperty("className");//相当于获取配置文件的key
String className1=props.getProperty("className1");
try {
Collection collections=(Collection)Class.forName(className).newInstance();//调用无参数的构造函数
Collection collections1=(Collection)Class.forName(className1).newInstance();
ReflectPoint p1=new ReflectPoint(3,3);
ReflectPoint p2=new ReflectPoint(5,5);
ReflectPoint p3=new ReflectPoint(3,3);
collections.add(p1);//将这些放入集合中去
collections.add(p2);
collections.add(p3);
collections.add(p1);
collections1.add(p1);
collections1.add(p2);
collections1.add(p3);
collections1.add(p1);
System.out.println("HashSet的长度= "+collections.size());
System.out.println("ArrayList的长度= "+collections1.size());//打印结果的同时可以看看两个类型储存数据的区别的哦亲
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;
public class ReflectDemo2 {
@SuppressWarnings("unchecked")
public static void main(String[] args){
try {
InputStream ips= new FileInputStream("D:/javaworkplace/heima/src/reflect/config.properties");//修改为自己的目录
Properties props=new Properties();
try {
props.load(ips);
} catch (IOException e) {
e.printStackTrace();
}
try {
ips.close();
String className=props.getProperty("className");//相当于获取配置文件的key
String className1=props.getProperty("className1");
try {
Collection collections=(Collection)Class.forName(className).newInstance();//调用无参数的构造函数
Collection collections1=(Collection)Class.forName(className1).newInstance();
ReflectPoint p1=new ReflectPoint(3,3);
ReflectPoint p2=new ReflectPoint(5,5);
ReflectPoint p3=new ReflectPoint(3,3);
collections.add(p1);//将这些放入集合中去
collections.add(p2);
collections.add(p3);
collections.add(p1);
collections1.add(p1);
collections1.add(p2);
collections1.add(p3);
collections1.add(p1);
System.out.println("HashSet的长度= "+collections.size());
System.out.println("ArrayList的长度= "+collections1.size());//打印结果的同时可以看看两个类型储存数据的区别的哦亲
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
如此便完成了一个小型的可以解析Collection类型子类的框架
---------------------- android培训、java培训、期待与您交流! ----------------------