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

How to use the reflection mechanism

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to use the reflection mechanism". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

All technologies achieve only one goal: reusability.

For reflection technology, the first consideration is the "negative" and "positive" operations. the so-called "positive" operation means that when using a class, you must first import the package where the program is located, and then instantiate the object according to the class. and rely on the object to call the method in the class. But "inverse" deduces its type based on the instantiated object.

Example: forward operation

Import java.util.Date; / / 1, the package where the importer is located. Class, knowing the origin of the object public class JavaAPIDemo {public static void main (String [] args) throws Exception {Date date=new Date (); / 2, generating the instantiated object System.out.println (date.getTime ()) through the class; / / 3, calling the method in the class} based on the object

Execution result:

If you want to implement inverse processing, the first thing you need to do is to take a method provided in the Object class:

Get Class object information: public final Class getClass ()

Example: observe the use of Class objects

Import java.util.Date; / / 1, the package where the importer is located. Class, knowing the origin of the object public class JavaAPIDemo {public static void main (String [] args) throws Exception {Date date=new Date (); / 2, generating the instantiated object System.out.println (date.getClass ()) through the class; / / 3. Finding the type of the object based on the instantiated object}}

GetClass () can help the user find the source of the object.

That's all for "how to use the reflection Mechanism". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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