31 反射方式给类的属性赋值 和 对象赋值(clone)
1、配置类
package com.da.tool.util.configuration.reflect; /** */ public class JobInfo { private String jobName; private String jobId; public String getJobName() { return jobName; } public void setJobName(String jobName) { this.jobName = jobName; } public String getJobId() { return jobId; } public void setJobId(String jobId) { this.jobId = jobId; } } package com.da.tool.util.configuration.reflect; /** */ public class TimedJob extends JobInfo { private String startTime; private String stopTime; public String getStartTime() { return startTime; } public void setStartTime(String startTime) { this.startTime = startTime; } public String getStopTime() { return stopTime; } public void setStopTime(String stopTime) { this.stopTime = stopTime; } }
2、反射类
package com.da.tool.util.configuration.reflect; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Date; import java.util.Properties; import java.util.Set; /** */ public class Reflect { public static void main(String[] args) { Properties prop = new Properties(); prop.setProperty("jobName", "timingJob"); prop.setProperty("jobId", "1"); prop.setProperty("startTime", String.valueOf(new Date().getTime())); prop.setProperty("stopTime", String.valueOf(new Date().getTime() + 1000 * 60 * 60 * 24)); JobInfo jobInfo = (JobInfo) setTarget(prop,new TimedJob()); TimedJob timedJob=(TimedJob) jobInfo; System.out.println("jobName = " + timedJob.getJobName()); System.out.println("jobId = " + timedJob.getJobId()); System.out.println("startTime = " + timedJob.getStartTime()); System.out.println("stopTime = " + timedJob.getStopTime()); } /** * 给实例instance的属性赋值 * * @param prop * @param instance * @return */ public static Object setTarget(Properties prop, Object instance) { Set<Object> keys = prop.keySet(); for (Object key : keys) { String name = String.valueOf(key); String value = prop.getProperty(String.valueOf(key)); try { Method method=instance.getClass().getMethod("set" + upperFirstChar(name), String.class); method.invoke(instance,value); } catch (NoSuchMethodException e) { //set方法不存在 e.printStackTrace(); } catch (InvocationTargetException e) { // e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }} return instance; } /** * 将字符串的首字母大写 * * @param string * @return */ public static String upperFirstChar(String string) { return string.substring(0, 1).toUpperCase() + string.substring(1); } }
jobName = timingJob jobId = 1 startTime = 1485152366304 stopTime = 1485238766304
2、对象赋值(clone)
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<surefire.redirectTestOutputToFile>true</surefire.redirectTestOutputToFile>
<groovy.version>2.1.6</groovy.version>
<slf4j.version>1.7.5</slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
package com.da.tool.util.configuration.reflect; /** */ public class JobInfo implements Cloneable{ private String jobName; private String jobId; public String getJobName() { return jobName; } public void setJobName(String jobName) { this.jobName = jobName; } public String getJobId() { return jobId; } public void setJobId(String jobId) { this.jobId = jobId; } @Override public Object clone() { JobInfo ic = null; try{ ic = (JobInfo)super.clone(); }catch(CloneNotSupportedException e) { e.printStackTrace(); } return ic; } }
测试类
package com.da.tool.util.configuration.clone; import com.da.tool.util.configuration.reflect.TimedJob; import org.apache.commons.lang.ObjectUtils; /** */ public class CloneInstance { public static void main(String[] args) { TimedJob timedJob=new TimedJob(); timedJob.setJobName("jobname"); timedJob.setJobId("1"); timedJob.setStartTime("123"); timedJob.setStopTime("789"); TimedJob cloneTimedJob1= (TimedJob)ObjectUtils.clone(timedJob); TimedJob cloneTimedJob2= (TimedJob)ObjectUtils.clone(timedJob); System.out.println("jobName = " + cloneTimedJob1.getJobName()); System.out.println("jobId = " + cloneTimedJob1.getJobId()); System.out.println("startTime = " + cloneTimedJob1.getStartTime()); System.out.println("stopTime = " + cloneTimedJob1.getStopTime()); System.out.println("jobName = " + cloneTimedJob1.getJobName()); System.out.println("jobId = " + cloneTimedJob1.getJobId()); System.out.println("startTime = " + cloneTimedJob1.getStartTime()); System.out.println("stopTime = " + cloneTimedJob1.getStopTime()); } }
测试输出
jobName = jobname
jobId = 1
startTime = 123
stopTime = 789
jobName = jobname
jobId = 1
startTime = 123
stopTime = 789
本文来自博客园,作者:life_start,转载请注明原文链接:https://www.cnblogs.com/yangh2016/p/6344028.html