当前位置:网站首页>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
边栏推荐
猜你喜欢

Set interface

XXL job parameter transfer

js真伪数组转换

2022 global Vocational Education Industry Development Report

Unity 2D game tutorial

Distributed system architecture theory and components

JS true / false array conversion

4. Analysis of the execution process of make modules for general purposes

Complete data summary of lapsus$apt organization that stole Microsoft's source code in March 2022

Will causal learning open the next generation of AI? Chapter 9 Yunji datacanvas officially released the open source project of ylarn causal learning
随机推荐
Configuration files in MySQL
Detail try catch finally
Set interface
About typora's inability to log in after March 9, 2022 -- resolved
5V boost 9V chip
概述静态内部类与非静态内部类
Aike AI frontier promotion (7.27)
BSP视频教程第21期:轻松一键实现串口DMA不定长收发,支持裸机和RTOS,含MDK和IAR两种玩法,比STM32CubeMX还方便(2022-07-24)
Common usage of curl command
Time tool class, get the current time, date to string
20210512 recursive formula
Error: the source of the existing CacheManager is: urlconfigurationsource
flinksql从Oracle同步数据到Doris,一共50几个字段,Oracle表中3000多万条
"Game engine light in light out" 4.1 unity shader and OpenGL shader
NFT mall /nft blind box / virtual blind box /nft transaction / customizable second opening
「游戏引擎 浅入浅出」4.1 Unity Shader和OpenGL Shader
Interviewer: how to deal with the data loss of redis master-slave cluster switching?
爬虫
GAN:生成对抗网络 Generative Adversarial Networks
Necessary foundation: Signature Verification