In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to obtain and use Class objects by Java". In the operation of actual cases, many people will encounter such a dilemma, so 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!
I. the basic concept of reflection
1. The concept of reflection
Encapsulating each component of a class into other objects is the mechanism of reflection.
We can get the Class object of the Person class through reflection, and get the member variables, constructors and member methods by getting the Class object, which is also the purpose of using reflection.
2. Benefits of using reflection
You can operate these objects in the process of running the program, and get the properties, methods and so on of class objects.
It can be decoupled to improve the scalability of the program.
3. Supplement
Reflection: the soul of frame design, and reflection is the key point of frame design.
So what is the frame when you know the reflection?
Framework: semi-finished software, we can use some packaged classes on the basis of the framework in the development process, so we can simplify the coding.
Second, get the Class object
1. How to get the Class object
Class.forname ("full class name"): loads the bytecode file into memory and returns the class object
Class name .class: obtained from the attribute class of the class name
Object .getclass (): because the getclass () method is defined in the object class, and because all classes inherit the object class, all classes have this method.
2. Demonstrate the above three ways to get objects
Person class
Public class Person {}
Main class
Public class Main {public static void main (String [] args) throws Exception {Class person = Class.forName ("test.Person"); Class person1 = Person.class; Person p = new Person (); Class person2 = p.getClass (); System.out.println (person); System.out.println (person1); System.out.println (person2);}}
Conclusion: the same bytecode file (* .class) will only be loaded once during a program run, and the class object obtained by either way is the same.
Third, use the class object function
Because all methods are used in a similar way, I will only demonstrate the method of getting member variables here.
Person class
Public class Person {public String name; private int age; public String sex;}
Main class
Public class Main {public static void main (String [] args) throws Exception {Class person = Class.forName ("test.Person"); Field [] fields = person.getFields (); System.out.println ("getFields (): only public attributes can be obtained"); for (int I = 0 Tinci
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.