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 > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you what the reflection mechanism of Java is and how to obtain the relevant knowledge. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
Java reflection mechanism
Java reflection mechanism is in the running state, for any class, all the properties and methods of this class can be obtained, and any one of its properties and methods can be called for any object. This function of dynamically fetching information at run time and dynamically calling methods of objects is called the reflection mechanism of Java.
Together with the java.lang.reflect class library, the Class class supports the concept of reflection, which contains Field,Method,Constructor classes (each class implements the Member interface). These types of objects are created at run time by JVM to represent the corresponding members of the unknown class. This allows you to create a new object using Constructor, read and modify the fields associated with the Field object with the get () and set () methods, and call the methods associated with the Method object with the invoke () method.
In addition, convenient methods such as getFields () getMethods () and getConstructors () can be called to return an array of objects that represent fields, methods, and constructors. So that the information of anonymous objects
It can be fully determined at run time, and you don't need to know anything at compile time.
Package com.microSpringboot.ReadingList.reflect;import java.lang.reflect.Constructor;public class ReflectTest {public static void main (String [] args) throws Exception {Class clazz = null; clazz = Class.forName ("com.microSpringboot.ReadingList.reflect.Fruit"); Constructor constructor1 = clazz.getConstructor (); Constructor constructor2 = clazz.getConstructor (String.class); Fruit fruit1 = constructor1.newInstance (); Fruit fruit2 = constructor2.newInstance ("Apple") }} class Fruit {public Fruit () {System.out.println ("no parameter constructor Run.");} public Fruit (String type) {System.out.println ("parameter constructor Run." + type);}} run result: no parameter constructor Run. .. There is a parameter constructor Run. .. three methods for AppleJava to obtain reflection
1. Implement reflection mechanism through new object
two。 Implementation of reflection mechanism through path
3. Implement reflection mechanism through class name
Package com.microSpringboot.ReadingList.reflect;public class Student {private int id; private String name; protected boolean sex; public float score;} package com.microSpringboot.ReadingList.reflect;public class Get {/ / three ways to obtain reflection mechanism public static void main (String [] args) throws ClassNotFoundException {/ / (by creating an object) Student student = new Student (); Class stuClass1 = student.getClass () System.out.println (stuClass1.getName ()); / / Mode 2 (path-relative path) Class stuClass2 = Class.forName ("com.microSpringboot.ReadingList.reflect.Student"); System.out.println (stuClass2.getName ()); / / Mode 3 (via class name) Class stuClass3 = Student.class; System.out.println (stuClass3.getName ()) }} above is all the content of the article "what is the reflection mechanism of Java and how to get it?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.