In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use Java reflection technology", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to use Java reflection technology" this article.
1. Basic reflection technique 1.1 gets a getClass-like method String name = "Huanglinqing" from a string; Class C1 = name.getClass (); System.out.println (c1.getName ())
The print result is as follows:
Class.forName
For example, we get the class name of java.lang.String
String name = "java.lang.String"; Class C1 = null; try {C1 = Class.forName (name); System.out.println (c1.getName ());} catch (ClassNotFoundException e) {}
This is also done by catching exceptions, because the string we passed may not be legal, and the legal naming of the string is composed of the namespace of the class and the name of the class.
The print result is as follows:
We can also get his parent class through c1.getSuperclass ()
Type attribute
All basic types have a type attribute, and you can get the type of this basic type, such as:
Class C1 = Boolean.TYPE;Class c2 = Byte.TYPE;Class c3 = Float.TYPE;Class c4 = Double.TYPE; II, get the members of the class
Can we call when the method in the class is defined as private? I can't! Can we get it when the variable is private? I can't! But reflection can, for example, there are methods you need to use in the source code, but that method is private, so you can execute the private method through reflection and get private variables.
Get the constructor of the class
For testing purposes, we define a Test class. The Test class is as follows: (omit the get and set methods)
In the Test class, we define three private variables that generate two public parametric constructors, a private parametric constructor and a public parameterless constructor.
Public class Test {private int age; private String name; private int testint; public Test (int age) {this.age = age;} public Test (int age, String name) {this.age = age; this.name = name;} private Test (String name) {this.name = name;} public Test () {}
Let's get these constructors through reflection.
Get all the constructors of the class
Test test = new Test (); Class c4 = test.getClass (); Constructor [] constructors; constructors = c4.getDeclaredConstructors ()
Through getDeclaredConstructors, you can return all the constructors of the class, and return an array. Because there may be more than one constructor, you can get the type of the constructor through getModifiers, and getParameterTypes can get all the parameters of the constructor and return a Class array, so if you want to get all the constructors and the parameter types of each constructor, you can have the following code:
For (int I = 0; I
< constructors.length; i++) { System.out.print(Modifier.toString(constructors[i].getModifiers()) + "参数:"); Class[] parametertypes = constructors[i].getParameterTypes(); for (int j = 0; j < parametertypes.length; j++) { System.out.print(parametertypes[j].getName() + " "); } System.out.println(""); } 运行结果如下所示: 这样我们就得到了类中所有构造方法和构造方法中的参数,那么我们如何获取特定的构造方法呢? 获取类中特定的构造方法 我们可以通过getConstructors方法获取类中 所有的public类型的构造方法,代码和上面一样就不演示了。 我们可以通过getDeclaredConstructor()方法传参获取特定参数类型的构造方法,这里注意是getDeclaredConstructor()不是 getDeclaredConstructors() ,所以返回的是一个Class对象而不是一个Class数组。 获取无参构造方法直接不传参数,如下所示: try { constructors = c4.getDeclaredConstructor(); System.out.print(Modifier.toString(constructors.getModifiers()) + ); } catch (NoSuchMethodException e) { e.printStackTrace(); } 这里要进行异常捕获,因为可能不存在对应的构造方法,打印结果如下: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.