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 the knowledge of Java reflection mechanism?

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what is the knowledge of Java reflection mechanism". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the knowledge of Java reflection mechanism"?

What is the class class

In an object-oriented world, everything is an object. A class is an object, and a class is an instance object of the java.lang.Class class. In addition, only the java virtual machine can new the class class. Any class is an instance object of the Class class. This instance object can be expressed in three ways:

Public class User {} public class ClassTest {User u=new User (); / / Mode 1: Class c1roomUser.class; / / Mode 2: Class c2=u.getClass (); / / Mode 3: Class c3=Class.forName ("com.forezp.User"); / / you can create an instance object of the class User user= (User) User.class () through the type of the class;}

II. Dynamic loading of class

Class.forName (the full name of the class); this method represents not only the type of the class, but also the dynamically loaded class. The compile-time load class is statically loaded, and the run-time load class is dynamically loaded.

Third, obtain method information

The basic data types and the void keyword are instances of the Class class; you can get the name of the class through getame (); getSimpleName ().

Class c1roomString.class; Class c2roomint.class; Class c3void.class; System.out.println (c1.getName ()); System.out.println (c2.getSimpleName ())

Get all the methods of the class and print them out:

Public static void printClassInfo (Object object) {

Class c=object.getClass ()

System.out.println ("name of class:" + c.getName ())

/ * *

* A member method is a method object

* getMethod () all public methods, including public inherited by the parent class

* getDeclaredMethods () gets all the methods of the class, including private, but not the inherited methods.

, /

Method [] methods=c.getMethods (); / / get method

/ / get all the methods, including private, c.getDeclaredMethods ()

For (int iTuno Bandi)

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