Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is single-level attribute assignment

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what is single-level attribute assignment". In daily operation, I believe that many people have doubts about what single-level attribute assignment is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the question of "what is single-level attribute assignment"! Next, please follow the editor to study!

Single-level attribute configuration

For the Emp class at this time, you will find that there are no other reference associations for the given data types, but only describe the objects of the Empty class, so this setting is called single-level setting processing, so you should deal with two things at this time:

Need to instantiate the specified class object through reflection

Set the content (Field property type, method name, content to be set)

Reflection and simple Java classes

1. Define StringUtils to realize the function of uppercase initials.

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);}

2. Define the BeanUtils tool class, which mainly implements the setting of properties.

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 | property" * / public static void setValue (Object obj,String value) {String results [] = value.split ("\\ |") / / split each set of attributes according to "|" for (int x = 0; x < results.length; x + +) {/ / Loop setting property content / / attval [0] holds the attribute name, and attval [1] holds the attribute content String attval [] = resultsx] .split (":") / / get "attribute name" and content try {Field field = obj.getClass () .getDeclaredField (attval [0]); / / get member Method setMethod = obj.getClass () .getDeclaredMethod ("set" + StringUtils.initcap (attval [0]), field.getType ()); setMethod.invoke (obj, attval [1]); / / call setter method to set content} catch (Exception e) {}

3. ClassInstanceFactory is responsible for instantiating the object and calling the BeanUtils class to set the property content.

Class ClassInstanceFactory {private ClassInstanceFactory () {} / * instantiates the creation method of the object, which can be based on the passed string structure: "attribute: content | property: content" * @ param clazz reflects the instantiated Class object 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 throwing an exception does not use return null;}

No matter how many attributes in the class, you can easily implement the call to setter (class object instantiation processing).

At this point, the study of "what is single-level attribute assignment" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report