-------------------------------------------------android培训、java培训期待与您交流!----------------------------------
一、使用反射克隆对象
public class ObjectCopyTest
{
public static Object objectCopy(Object object) throws Exception
{
//得到这个对象的class封装信息对象
Class<?> className=object.getClass();
//生存一样信息的新对象,通过无参构造函数构造
Object newObject=className.newInstance();
//得到这个类的所有属性
Field[] field=className.getDeclaredFields();
//进行遍历
for(Field f:field)
{
//所有属性第一个字母变大写
String s=f.getName().substring(0,1).toUpperCase()+f.getName().substring(1);
//得到每个属性的访问方法
Method mGet=className.getMethod("get"+s, new Class[]{});
Method mSet=className.getMethod("set"+s, new Class[]{f.getType()});
//传入对象,得到传入对象的信息
Object o=mGet.invoke(object, new Object[]{});
//将它赋给新的对象
mSet.invoke(newObject, new Object[]{o});
}
//最后返回
return newObject;
}
public static void main(String[] args) throws Exception
{
People p=(People)ObjectCopyTest.objectCopy(new People("le",19));
System.out.println("---------------------------------");
System.out.println(p.name+"\t"+p.age);
}
}
class People
{
String name;
int age;
public People()
{
}
public People(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}
二、使用反射更改字段内容
public class ReflectApp {
public static void main(String[] args) throws Exception {
Student s=new Student();
Class<?> clazz=s.getClass();
Field[] fields=clazz.getDeclaredFields();
for (Field field : fields) {
//getType()字段类型
if(field.getType()==String.class)
{
field.setAccessible(true);
String oldStr=(String) field.get(s);
String newStr=oldStr.replace("b", "a");
field.set(s, newStr);
}
}
System.out.println(s.getFirst_name());
System.out.println(s.getSecond_name());
System.out.println(s.getThree_name());
}
}
class Student
{
private String first_name="abc";
private String second_name="bb";
private String three_name="aa";
private int age=12;
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getSecond_name() {
return second_name;
}
public void setSecond_name(String second_name) {
this.second_name = second_name;
}
public String getThree_name() {
return three_name;
}
public void setThree_name(String three_name) {
this.three_name = three_name;
}
}
三、框架基础
//使用传统的加载方式
//InputStream is=new FileInputStream("bin/com/em/reflectLoader/resource/config.properties");
//通过类加载器
InputStream is=ReflectLoader.class.getClassLoader().getResourceAsStream("com/em/reflectLoader/resource/config.properties");
//使用快捷的方式,内部还是调用了类加载器,这里是相对本类的路径
is=ReflectLoader.class.getResourceAsStream("resource/config.properties");
Properties props=new Properties();
props.load(is);
String className=props.getProperty("className");
List<String> lists=(List<String>)Class.forName(className).newInstance();
lists.add("Em");
lists.add("Gm");
System.out.println(lists.size());
-------------------------------------------------android培训、java培训期待与您交流!----------------------------------