In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use Java reflection", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Java reflection" this article.
1. What is a reflex?
Reflection is actually the dynamic loading of classes, which is involved when we load the driver Class.forName ("xxx") when we write JDBC.
The reflection mechanism is that in the running state, for any class, all the properties and methods of this class can be known; for any object, any one of its methods and properties can be called; this dynamic information and the function of dynamically calling the methods of the object is called the reflection mechanism of java language.
two。 What can the reflection mechanism do?
1. Determine the class Class that any object belongs to at runtime.
2. Judge the object Constructor that constructs any class at run time.
3. Determine the member variable Field and method Method of any class at run time.
4. Call the method of any object at run time. Method.invoke (object,args)
3. The API related to the reflection mechanism gets the complete package name and class name
Student.java
Package com.reflect.test;public class Student {private int id; private String name; public Student () {} public Student (int id, String name) {super (); this.id = id; this.name = name;} public int getId () {return id;} public void setId (int id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name @ Override public String toString () {return "Student [id=" + id + ", name=" + name + "]";}}
Test.java
Package com.reflect.test;public class Test {Student stu = new Student (); public static void main (String [] args) {Test test = new Test (); test.getClassName ();} / / get the complete package name and class name public void getClassName () {System.out.println (stu.getClass (). GetName ());}}
Running result
Com.reflect.test.Student
Three ways to get Class
Public static void main (String [] args) {Test test = new Test (); test.getStudentClass ();} / / three violations of Class object public void getStudentClass () {/ / first method, forName Class class1; try {class1 = Class.forName ("com.reflect.test.Student"); System.out.println ("class1:" + class1);} catch (ClassNotFoundException e) {e.printStackTrace ();} / second method, class Class class2 = Student.class System.out.println ("class2:" + class2); / / third method, getClass () Student stu1 = new Student (); Class class3 = stu1.getClass (); System.out.println ("class3:" + class3);}
Running result
Class1:class com.reflect.test.Studentclass2:class com.reflect.test.Studentclass3:class com.reflect.test.Student
The above is all the contents of the article "how to use Java reflection". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.