In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "what are the ways to obtain reflection in Java". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what are the ways to obtain reflection in Java" can help you solve the problem.
Preface
To quote a classic saying.
Reflection is the soul of frame design. Only when we learn reflection well can we design a good framework.
What is the reflection mechanism?
The reflection mechanism in Java is that at run time, for any class, you can know all the properties and methods in this class; for any object, you can call any method and property in this object. This function of dynamically obtaining information and dynamically calling object methods is called the reflection mechanism in Java.
The role of reflection
(1) get the variables of some classes and call the private methods of some classes.
(2) increase the flexibility of the code.
Reflect common object
(1) an instance of the Class Class class represents the classes and interfaces in the running Java application.
(2) Constructor corresponds to the constructor in a class or object, information about a single constructor of the class, and access to it.
(3) Field corresponds to the variables in the class or object, providing information about a single field of the class or interface, as well as dynamic access to it.
(4) Method corresponds to a method in a class or object and provides information about a single method on the class or interface
Three common ways to obtain reflection
(1) the reflection mechanism is realized by creating objects with new keywords.
(2) the reflection mechanism is realized through the path.
(3) the reflection mechanism is realized through the class name.
Concrete examples
Set up a student class
Public class Student {private int id; String name; protected boolean sex; public float score;}
Get reflection mechanism
Public class Get {/ / three ways to get reflection mechanism public static void main (String [] args) throws ClassNotFoundException {/ / way one (by creating an object) Student stu = new Student (); Class classobj1 = stu.getClass (); System.out.println (classobj1.getName ()) / / Mode 2 (via path-relative path) Class classobj2 = Class.forName ("fanshe.Student"); System.out.println (classobj2.getName ()); / / Mode 3 (via class name) Class classobj3 = Student.class System.out.println (classobj3.getName ());}} that's all about how to get reflection in Java. Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.