利用反射给对象按顺序赋值
需求:某一个类有属性1~12月,赋值顺序为5月,6月,7月,8月,9月,10月,11月,12月,1月,2月,3月,4月;输入开始月份(val1)和结束月份(val2)使得期间的所有月份赋值为1。
基本思路:通过反射获取Demo类的所有月份的setter,然后按以上顺序放到Method[ ] methods数组里,根据输入的月份区间顺序地执行setter方法。
Demo类:
1 public class Demo { 2 //5月 3 private int May; 4 5 //6月 6 private int June; 7 8 //7月 9 private int July; 10 11 //8月 12 private int August; 13 14 //9月 15 private int September; 16 17 //10月 18 private int October; 19 20 //11月 21 private int November; 22 23 //12月 24 private int December; 25 26 //1月 27 private int January; 28 29 //2月 30 private int February; 31 32 //3月 33 private int March; 34 35 //4月 36 private int April; 37 38 public void setMay(int may) { 39 May = may; 40 } 41 42 public void setJune(int june) { 43 June = june; 44 } 45 46 public void setJuly(int july) { 47 July = july; 48 } 49 50 public void setAugust(int august) { 51 August = august; 52 } 53 54 public void setSeptember(int september) { 55 September = september; 56 } 57 58 public void setOctober(int october) { 59 October = october; 60 } 61 62 public void setNovember(int november) { 63 November = november; 64 } 65 66 public void setDecember(int december) { 67 December = december; 68 } 69 70 public void setJanuary(int january) { 71 January = january; 72 } 73 74 public void setFebruary(int february) { 75 February = february; 76 } 77 78 public void setMarch(int march) { 79 March = march; 80 } 81 82 public void setApril(int april) { 83 April = april; 84 } 85 }
oneTest赋值&测试类:
1 public class oneTest { 2 public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { 3 oneTest test = new oneTest(); 4 5 //9月到明年2月 6 Demo demo = test.Myfunction(9, 2); 7 System.out.println(demo); 8 } 9 10 11 //给连续的月份赋值 12 public Demo Myfunction(int val1,int val2) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{ 13 //5---12 减5变成 0---7 ; 1---4 加7变成 8---11 14 int start = val1 >= 5 && val1 <=12?val1-5:val1+7; 15 int end = val2 >= 5 && val2 <=12?val2-5:val2+7; 16 Demo demo = new Demo(); 17 Class clazz = Demo.class; 18 //空数组 19 Method[] methods =new Method[12]; 20 21 //通过反射得到Demo类的方法 22 Method[] tempmethods =clazz.getDeclaredMethods(); 23 24 //给方法排序5月---12月---4月 25 for(int i = 0 ; i < tempmethods.length ; i++) { 26 if(tempmethods[i].getName().equals("setMay")) { 27 methods[0] = tempmethods[i]; 28 } 29 if(tempmethods[i].getName().equals("setJune")) { 30 methods[1] = tempmethods[i]; 31 } 32 if(tempmethods[i].getName().equals("setJuly")) { 33 methods[2] = tempmethods[i]; 34 } 35 if(tempmethods[i].getName().equals("setAugust")) { 36 methods[3] = tempmethods[i]; 37 } 38 if(tempmethods[i].getName().equals("setSeptember")) { 39 methods[4] = tempmethods[i]; 40 } 41 if(tempmethods[i].getName().equals("setOctober")) { 42 methods[5] = tempmethods[i]; 43 } 44 if(tempmethods[i].getName().equals("setNovember")) { 45 methods[6] = tempmethods[i]; 46 } 47 if(tempmethods[i].getName().equals("setDecember")) { 48 methods[7] = tempmethods[i]; 49 } 50 if(tempmethods[i].getName().equals("setJanuary")) { 51 methods[8] = tempmethods[i]; 52 } 53 if(tempmethods[i].getName().equals("setFebruary")) { 54 methods[9] = tempmethods[i]; 55 } 56 if(tempmethods[i].getName().equals("setMarch")) { 57 methods[10] = tempmethods[i]; 58 } 59 if(tempmethods[i].getName().equals("setApril")) { 60 methods[11] = tempmethods[i]; 61 } 62 } 63 64 //执行相应的set方法 65 for(int i = start;i <= end ; i++) { 66 methods[i].invoke(demo, 1); 67 } 68 return demo; 69 } 70 }
输出结果:
Demo [May=0, June=0, July=0, August=0, September=1, October=1, November=1, December=1, January=1, February=1, March=0, April=0]