欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

设置多种数据类型,级联对象实例化,级联属性赋值

程序员文章站 2023-10-16 20:17:04
......

 

一个简单java类中的属性类型不仅仅只有string,还会包含有整数、浮点数、日期等,本课程主要讲解如何实现多种数据类型的赋值以及转换处理操作。
一个类可以与其它类发生引用关系,以描述彼此之间的关系,这样的级联结构中就需要考虑对象实例化问题,本课程主要讲解如何在多级vo配置关系时如何通过反射技术实现动态实例化对象操作。
类引用定义之后就会存在有其它引用类型的属性赋值操作,本课程主要讲解多级实例化对象属性内容的获取与其属性设置。
  1 package com.twitter.demo;
  2 
  3 import java.util.date;
  4 import java.lang.reflect.field;
  5 import java.lang.reflect.method;
  6 import java.text.parseexception;
  7 import java.text.simpledateformat;
  8 
  9 class company{
 10     private string name;
 11     private date createdate;
 12     public string getname() {
 13         return this.name;
 14     }
 15     public void setname(string name) {
 16         this.name = name;
 17     }
 18     public date getcreatedate() {
 19         return this.createdate;
 20     }
 21     public void setcreatedate(date createdate) {
 22         this.createdate = createdate;
 23     }
 24 }
 25 
 26 class dept{
 27     private string dname;
 28     private string loc;
 29     private company company;
 30     public string getdname() {
 31         return dname;
 32     }
 33     public void setdname(string dname) {
 34         this.dname = dname;
 35     }
 36     public string getloc() {
 37         return loc;
 38     }
 39     public void setloc(string loc) {
 40         this.loc = loc;
 41     }
 42     public company getcompany() {
 43         return company;
 44     }
 45     public void setcompany(company company) {
 46         this.company = company;
 47     }
 48 }
 49 
 50 class emp{
 51     private string ename;
 52     private string job;
 53     private double salary;
 54     private integer age;
 55     private date hiredate;
 56     private dept dept;
 57     public string getename() {
 58         return ename;
 59     }
 60     public void setename(string ename) {
 61         this.ename = ename;
 62     }
 63     public string getjob() {
 64         return job;
 65     }
 66     public void setjob(string job) {
 67         this.job = job;
 68     }
 69     public double getsalary() {
 70         return salary;
 71     }
 72     public void setsalary(double salary) {
 73         this.salary = salary;
 74     }
 75     public integer getage() {
 76         return age;
 77     }
 78     public void setage(integer age) {
 79         this.age = age;
 80     }
 81     public date gethiredate() {
 82         return hiredate;
 83     }
 84     public void sethiredate(date hiredate) {
 85         this.hiredate = hiredate;
 86     }
 87     public dept getdept() {
 88         return dept;
 89     }
 90     public void setdept(dept dept) {
 91         this.dept = dept;
 92     }
 93 }
 94 
 95 class beanutils{
 96     private beanutils() {}
 97     public static void setvalue(object obj,string value) {
 98         string results[] = value.split("\\|");
 99         for(int x=0;x<results.length;x++) {
100             string attval[] = results[x].split(":");
101             try {
102                 if(attval[0].contains(".")) {
103                     string temp[] = attval[0].split("\\.");
104                     object currentobject = obj;
105                     for(int y=0;y<temp.length-1;y++) {
106                         method getmethod = currentobject.getclass().getdeclaredmethod("get" + stringutils.initcap(temp[y]));
107                         object tempobject = getmethod.invoke(currentobject);
108                         if(tempobject == null) {
109                             field field = currentobject.getclass().getdeclaredfield(temp[y]);
110                             method method = currentobject.getclass().getdeclaredmethod("set"+stringutils.initcap(temp[y]), field.gettype());
111                             object newobject = field.gettype().getdeclaredconstructor().newinstance();
112                             method.invoke(currentobject, newobject);
113                             currentobject = newobject;
114                         }else {
115                             currentobject = tempobject;
116                         }
117                     }
118                 }else {
119                     field field = obj.getclass().getdeclaredfield(attval[0]);
120                     method setmethod = obj.getclass().getdeclaredmethod("set"+stringutils.initcap(attval[0]), field.gettype());
121                     object convertvalue = beanutils.convertatributevalue(field.gettype().getname(), attval[1]);
122                     setmethod.invoke(obj, convertvalue);
123                 }
124             } catch (exception e) {
125                 // todo: handle exception
126             }
127         }
128     }
129     
130     private static object convertatributevalue(string type,string value) {
131         if("long".equals(type)||"java.lang.long".equals(type)) {
132             return long.parselong(value);
133         }else if ("int".equals(type)||"java.lang.integer".equals(type)) {
134             return integer.parseint(value);
135         }else if ("double".equals(type)||"java.lang.double".equals(type)) {
136             return double.parsedouble(value);
137         }else if ("java.util.date".equals(type)) {
138             simpledateformat sdf = null;
139             if(value.matches("\\d{4}-\\d{2}-\\d{2}")) {
140                 sdf = new simpledateformat("yyyy-mm-dd");
141             }else if (value.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}-\\d{2}-\\d{2}")) {
142                 sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
143             }else {
144                 return new date();
145             }
146             try {
147                 return sdf.parse(type);
148             } catch (parseexception e) {
149                 return new date();
150             }
151         }else {
152             return value;
153         }
154     }
155 }
156 
157 class stringutils{
158     public static string initcap(string str) {
159         if(str == null || "".equals(str)) {
160             return str;
161         }
162         if(str.length() == 1) {
163             return str.touppercase();
164         }else {
165             return str.substring(0,1).touppercase()+str.substring(1);
166         }
167     }
168 }
169 
170 class classinstancefactory{
171     private classinstancefactory() {}
172     public static <t> t create(class<?> clazz,string value) {
173         try {
174             object obj = clazz.getdeclaredconstructor().newinstance();
175             beanutils.setvalue(obj, value);
176             return (t) obj;
177         } catch (exception e) {
178             return null;
179         }
180         
181     }
182 }
183 
184 public class javareflectdemo {
185     public static void main(string[] args) throws exception{
186         string value = "ename:smith|job:clerk|salary:8960.00|age:30|hiredate:2003-10-03|"
187                 +"dept.dname:财务部|dept.company.name:twitter|"
188                 +"dept.company.createdate:2001-11-11";
189         emp emp = classinstancefactory.create(emp.class, value);
190         system.out.println(emp);
191         system.out.println(emp.getdept());
192         system.out.println(emp.getdept().getcompany());
193     }
194 }