当前位置:网站首页>Detail the construction methods, attributes and common methods in reflection
Detail the construction methods, attributes and common methods in reflection
2022-07-27 12:42:00 【Rippling rippling】
How to get
How to get public Method of type , Methods that are not limited to this class also include methods that inherit from the parent class public Method
example 1
package keeper;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
//1. How to get public Methods that are not limited to this class also include methods that inherit from the parent class public Method
Class clazz=Student.class;
Method [] methods =clazz.getMethods();
for (Method method : methods) {
System.out.println(method.getName());
}
}
}
package keeper;
public class Student {
public static String name;
public void doHomework(int a,String [] sd) {
System.out.println(name+"fadfsf");
}
public Student() {
}
public Student(String name) {
this.name=name;
}
}result :
doHomework
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAllHow to get methods in this class
example 2
package keeper;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
Class clazz=Student.class;
Method method;
try {
method = clazz.getMethod("doHomework");
System.out.println(method);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package keeper;
public class Student {
public static String name;
public void doHomework() {
System.out.println(name+"fadfsf");
}
public Student() {
}
public Student(String name) {
this.name=name;
}
}
result :
public void keeper.Student.doHomework()example 3
package keeper;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
Class clazz=Student.class;
Method [] methods =clazz.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method.getName());
}
}
}
package keeper;
public class Student {
public static String name;
public void doHomework() {
System.out.println(name+"fadfsf");
}
public Student() {
}
public Student(String name) {
this.name=name;
}
}result :
doHomework
pick up information
example 4
package keeper;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class Test {
public static void main(String[] args) {
Class clazz=Student.class;
try {
Method method =clazz.getMethod("doHomework", Integer.TYPE,String[].class);
System.out.println(method.getDeclaringClass());
System.out.println(Modifier.toString(method.getModifiers()));
clazz =method .getReturnType();
System.out.println(clazz.getName());
System.out.println(method.getName());
Class [] clazzs =method.getParameterTypes();
for (Class str : clazzs) {
System.out.println(str.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package keeper;
public class Student {
public static String name;
public void doHomework(int a,String [] sd) {
System.out.println(name+"fadfsf");
}
public Student() {
}
public Student(String name) {
this.name=name;
}
}result :
class keeper.Student
public
void
doHomework
int
[Ljava.lang.String;example 5:
package keeper;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class Test {
public static void main(String[] args) {
Class clazz=Student.class;
try {
Method method =clazz.getDeclaredMethod("doHomework", Integer.TYPE,String[].class);
method.setAccessible(true);
Object object =method.invoke(new Student(), 1,new String[] {});
System.out.println(object);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package keeper;
public class Student {
public static String name;
private void doHomework(int a,String [] sd) {
System.out.println(name+"fadfsf");
}
public Student() {
}
public Student(String name) {
this.name=name;
}
}
result :
nullfadfsf
nullHow to use
attribute
1. How to get
a obtain public( Contains ) Level attribute
package keeper;
import java.lang.reflect.Field;
public class Test {
public static void main(String[] args) {
Class clazz=Student.class;
Field field;
try {
field = clazz.getField("name");
System.out.println(field.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package keeper;
public class Student {
public static String name;
private void doHomework(int a,String [] sd) {
System.out.println(name+"fadfsf");
}
public Student() {
}
public Student(String name) {
this.name=name;
}
}result :
name
b Getting this type of attribute does not distinguish permissions
package keeper;
import java.lang.reflect.Field;
public class Test {
public static void main(String[] args) {
Class clazz=Student.class;
Field field;
try {
field = clazz.getDeclaredField("name");
System.out.println(field.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package keeper;
public class Student {
public static String name;
private void doHomework(int a,String [] sd) {
System.out.println(name+"fadfsf");
}
public Student() {
}
public Student(String name) {
this.name=name;
}
}
result :
name
2. pick up information
package keeper;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class Test {
public static void main(String[] args) {
Class clazz=Student.class;
try {// class , Modifier , type , name
Method method =clazz.getDeclaredMethod("doHomework", Integer.TYPE,String[].class);
System.out.println(method.getDeclaringClass());
System.out.println(Modifier.toString(method.getModifiers()));
clazz =method .getReturnType();
System.out.println(clazz.getName());
System.out.println(method.getName());
Class [] clazzs =method.getParameterTypes();
for (Class str : clazzs) {
System.out.println(str.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package keeper;
public class Student {
public static String name;
public void doHomework(int a,String [] sd) {
System.out.println(name+"fadfsf");
}
public Student() {
}
public Student(String name) {
this.name=name;
}
}result :
class keeper.Student
public
void
doHomework
int
[Ljava.lang.String;3. Operation properties ( Assign and get values )
package keeper;
import java.lang.reflect.Field;
public class Test {
public static void main(String[] args) {
Class clazz=Student.class;
try {
Field field = clazz.getDeclaredField("name");
field.setAccessible(true);
Student student = new Student();
field.set(student, "sgfgsdgdsfg");
Object object =field.get(student);
System.out.println(object);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package keeper;
public class Student {
public static String name;
private void doHomework(int a,String [] sd) {
System.out.println(name+"fadfsf");
}
public Student() {
}
public Student(String name) {
this.name=name;
}
}result :
sgfgsdgdsfg
边栏推荐
- SSM实战项目-前后分离(简单易懂)
- V. introduction of other objectives and general options
- US pressure surges tiktok changes global safety director
- Error: the source of the existing CacheManager is: urlconfigurationsource
- What should I do if I can't see any tiles on SAP Fiori launchpad?
- Dominoes staged: the beginning and end of the three arrow capital crash
- 概述静态内部类与非静态内部类
- 分布式系统架构理论与组件
- BSP视频教程第21期:轻松一键实现串口DMA不定长收发,支持裸机和RTOS,含MDK和IAR两种玩法,比STM32CubeMX还方便(2022-07-24)
- Distributed system architecture theory and components
猜你喜欢

MySQL extensions

Map接口

20210519 leetcode double pointer

详述try-catch-finally

虚拟偶像的歌声原来是这样生成的!

The strongest distributed locking tool: redisson

Openpyxl drawing area map

CEPH distributed storage performance tuning (6)

Security measures for tcp/ip protocol vulnerabilities

Xposed+fdex2 app shelling (black cat complains about app shelling)
随机推荐
详述throw与throws
Detail the execution process of JDBC query method
2021-3-19-byte-face value
Getting started for beginners: build your own blog with WordPress
Xianghe meat cake in memory
Do you really understand CMS garbage collector?
ArrayList常用方法总结
20210519 leetcode double pointer
Error: slf4j: class path contains multiple slf4j bindings
一款能模糊的地方都能模糊的测试工具——Wfuzz
Security measures for tcp/ip protocol vulnerabilities
(10) STM32 - systick tick timer
US pressure surges tiktok changes global safety director
Set接口
II. Analysis of the execution process of make menuconfig
Pyside6/pyqt development experience summary (2) - set shortcut keys
C program debugging and exception handling (try catch)
Will causal learning open the next generation of AI? Chapter 9 Yunji datacanvas officially released the open source project of ylarn causal learning
Detail try catch finally
详述HashSet的add方法