In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to master ava reflection". In daily operation, I believe many people have doubts about how to master ava reflection. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to master ava reflection"! Next, please follow the editor to study!
Reflection mechanism
Java reflection is a key property of Java as a dynamic (or quasi-dynamic) language. This mechanism allows the program to obtain the internal information of any known name class and any object through Reflection APIs at run time. The Java reflection mechanism provides the following functions:
Determine the class to which any object belongs at run time
Construct an object of any class at run time
Determine the member variables and methods of any class at run time
Call the method of any object at run time
Create a new class object at run time
When using the reflection function of Java, we must first get the Class object of the class, and then get other objects through the Class object. As we all know, annotated @ Test in Junit4 represents a test case, and the essence of each test case is a method in the test class, that is:
@ Test public void test () {fail ("Not yet implemented");}
We know that in general, the method to call a class is to instantiate the class first, mark it as obj, and then call it through obj.test (). Here we think about a problem, Junit4 is a framework, in the process of running, the framework has no idea how many test cases have been defined by the user (although constrained by @ Test), it is obvious that the framework confirms the test cases at run time and invokes the test cases in some way, which is the nature of reflection-working at run time!
Class class and Class instance
We know that a class in Java is a template that describes the behavior and state of a class of objects, such as:
Class Person {} Person kevin=new Person (); Person mike=new Person ()
Person is the type of Kevin and Mike, that is, the description of Kevin and Mike.
Everything in Java is an object, so Person (custom class), String (class provided by JDK). What type is it? They are all objects of the Class class and are described by the Class class.
What is an example of Class? Is a class or interface, or, more strictly, bytecode in java (a .class file generated when a class or interface is compiled).
Introduction to commonly used API
Here we focus on the reflection technology about getting Class objects, accessing fields, calling methods, and calling API of constructors.
1. Get the Class object of the class
An instance of the Class (java.lang.Class) class represents the classes and interfaces in the running Java application. This Class instance is created inside JVM. If we look at the JDK source code, we can find that the constructor of the Class class is private. Only JVM can create a Class instance. Our own Java program cannot create a Class instance. Because JVM creates a corresponding Class instance for each loaded class, and stores all the information of the class in the instance, including class name, package name, parent class, implemented interface, all methods, fields, etc., if we get a Class instance, we can get all the information of the corresponding class through this Class instance. There are several ways to get the Class object of a class:
2. Get the Fields of the class
You can get a property of a class through the reflection mechanism, and then change the value of the property corresponding to an instance of the class. JAVA's Class class provides several methods to get the properties of the class.
3. Get the Method of the class
Get a method of a class through the reflection mechanism, and then call the method corresponding to an instance of the class. The Class class provides several methods to get the class.
4. Get the Constructor of the class
Get the constructor of a class through the reflection mechanism, and then call the constructor to create an instance of the class. The Class class provides several methods to get the constructor of the class.
Reflective API application
Write a class
Public class ReflectDemo {ReflectDemo () {System.out.println ("default constructor");} ReflectDemo (String p_para) {System.out.println ("parametric constructor");} public String myPara1= "public property"; protected String myPara2= "protected property"; private String myPara3= "private property" Public void test1 () {System.out.println ("this is public void parameterless method test1");} protected String test2 (String p_test2) {System.out.println ("this is protected void parameterized method test2"); returnp_test2;} private void test3 () {System.out.println ("this is privated parameterless method test3") }} create a new class instance
Call the newInstance method of the class's Class object, which invokes the object's default constructor. If there is no default constructor, the call fails, as follows:
Class classType = ReflectDemo.class; Object inst = classType.newInstance (); System.out.println (inst)
Call the newInstance method of the default Constructor object, as follows:
Class classType = ReflectDemo.class; Constructor constructor1 = classType.getConstructor (); Object inst = constructor1.newInstance (); System.out.println (inst)
Call the newInstance method of the Constructor object with parameters, as follows:
Constructor constructor2 = ReflectDemo.class.getDeclaredConstructor (String.class); Object inst = constructor2.newInstance ("test"); System.out.println (inst); calling method
Get the class Method object through reflection and get all the functions in the class.
String className = "com.lesson.reflect.ReflectDemo"; Class clas = Class.forName (className); Method [] a=clas.getDeclaredMethods (); for (int itemoswiti)
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.