In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is setting multiple data types". Friends who are interested may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is to set multiple data types"!
Set up multiple data types
Now the single-level property configuration has been successfully implemented, but there is still a practical situation to consider: the current given data type is only String. In the actual development, the attribute types in simple Java classes are generally optional: long (Long), int (Integer), double (Double), String, Date (date, date and time), so the current program code must be modified to achieve the configuration of various data types.
Since different types of content settings can be implemented, and the BeanUtils class mainly completes the property assignment processing, you can append a series of processing methods to this class.
Import java.lang.reflect.Field;import java.lang.reflect.Method;public class JavaAPIDemo {public static void main (String [] args) throws Exception {String value= "empno:7369 | ename:Smith | job:Clerk | salary:750.00 | hiredate:1989-10-10"; Emp emp = ClassInstanceFactory.create (Emp.class, value) System.out.println ("employee number:" + emp.getEmpno () + ", name:" + emp.getEname () + ", position:" + emp.getJob () + ", base salary:" + emp.getSalary () + ", date of employment:" + emp.getHiredate () }} class ClassInstanceFactory {private ClassInstanceFactory () {} / * instantiate the creation method of the object, which can be based on the passed string structure: "attribute: content | property: content" * @ param clazz reflects the Class object to be instantiated With Class, you can reflect the property content of the instantiated object * @ param value to be set to the object * @ return A Java object that has been configured with property content * / public static T create (Class clazz,String value) {/ / if you want to use reflection for simple Java class object property settings, the class must have a nonparametric construct try {Object obj = clazz.getDeclaredConstructor (). NewInstance () BeanUtils.setValue (); / / set the property return (T) obj; / / return object} catch (Exception e) {e.printStackTrace (); / / if an error really occurs at this time, essentially return null;} class StringUtils {public static String initcap (String str) {if (str = = null | | ".equals (str)) {return str } if (str.length () = = 1) {return str.toUpperCase ();} else {return str.substring (0,1) .toUpperCase () + str.substring (1) } class BeanUtils {/ / Bean-processed class private BeanUtils () {} / * implements the property setting of the specified object * @ param obj the instantiated object to be reflected * @ param value contains the string of the specified content Format "attribute: content | attribute: content" * / public static void setValue (Object obj,String value) {String results [] = value.split ("\\ |") / / split for for each set of attributes according to "|" (int x = 0; x
< results.length; x ++) { //循环设置属性内容//attval [0]保存的是属性名称,attval [1]保存的是属性内容String attval [] = results[x].split(":"); //获取"属性名称"和内容try { Field field = obj.getClass().getDeclaredField(attval[0]); //获取成员Method setMethod = obj.getClass().getDeclaredMethod("set" + StringUtils.initcap(attval [0]), field.getType()); Object convertValue = BeanUtils.convertAttributeValue(field.getType().getName(), attval[1]); setMethod.invoke(obj, convertValue); //调用setter方法设置内容}catch (Exception e) { } } }/** * 实现属性类型转换处理 * @param type 属性类型,通过Field获取 * @param value 属性的内容,传入的都是字符串,需要将其变为指定类型 * @return 转换后的数据类型 */private static Object convertAttributeValue(String type, String value) {if ("long".equals(type) || "java.lang.Long".equals(type)) { //长整型return Long.parseLong(value); }else if ("int".equals(type) || "java.lang.int".equals(type)) {return Integer.parseInt(value); }else if ("double".equals(type) || "java.lang.double".equals(type)) {return Integer.parseDouble(value); }else if ("java.util.Date".equals(type)) { SimpleDateFormat sdf = null;if (value.matches("\\d{4}-\\d{2}-\\d{2}") { //日期类型sdf = new SimpleDateFormat("yyyy-MM-dd"); } else if (value.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}") { sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); }else {return new Date() ; //当前日期}try {return sdf.parse(value); } catch(ParseException e) {return new Date() ; //当前日期} }else {return value; } }}class Emp{private long empno;private String ename;private String job;private double salary;private Date hiredate;public void setEname(String ename) {this.ename = ename; }public void setJob(String job) {this.job = job; }public String getEname() {return ename; }public String getJob() {return job; }public long getEmpno() {return empno; }public void setEmpno(Long empno) {this.empno = empno; }public double getSalary() {return salary; }public void setSalary(double salary) {this.salary = salary; }public Date getHiredate() {return hiredate; }public void setHiredate(Date hiredate) {this.hiredate = hiredate; }} 执行结果:At this time, only a few commonly used data types are listed, and if you want to promote them as a product, you must consider all possible types, as well as all possible date formats.
At this point, I believe you have a deeper understanding of "what is to set multiple data types". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.