System.out.println(c2.getSimpleName());//The name of the class that does not contain the package name
System.out.println(c5.getName());
}
}
ClassDemo3.java
public class ClassDemo3 {
public static void main(String[] args) {
String s = "hello";
ClassUtil.printClassMethodMessage(s);
Integer n1 = 1;
ClassUtil.printClassMethodMessage(n1);
}
}
ClassDemo4.java
public class ClassDemo4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassUtil.printFieldMessage("hello");
System.out.println("=============");
ClassUtil.printFieldMessage(new Integer(1));
}
}
ClassDemo5.java
public class ClassDemo5 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassUtil.printConMessage("hello");
ClassUtil.printConMessage(new Integer(1));
}
}
ClassUtil.java
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ClassUtil {
/**
* Print class information, including class member functions and member variables(Get member functions only)
* @param obj Information about the class to which the object belongs
*/
public static void printClassMethodMessage(Object obj){
//To get class information, first get the class type of the class
Class c = obj.getClass();//The object c of which subclass is passed is the class type of the subclass
//Gets the name of the class
System.out.println("The name of the class is:"+c.getName());
/*
* Method Class, method object
* A member method is a Method object
* getMethods()Method gets all public Functions, including those inherited from the parent class
* getDeclaredMethods()Get all the methods declared by the class, regardless of access rights
*/
Method[] ms = c.getMethods();//c.getDeclaredMethods()
for(int i = 0; i < ms.length;i++){
//Get the class type of the return value type of the method
Class returnType = ms[i].getReturnType();
System.out.print(returnType.getName()+" ");
//Get the name of the method
System.out.print(ms[i].getName()+"(");
//Get the parameter type -- > get the class type of the type of the parameter list
Class[] paramTypes = ms[i].getParameterTypes();
for (Class class1 : paramTypes) {
System.out.print(class1.getName()+",");
}
System.out.println(")");
}
}
/**
* Get information about member variables
* @param obj
*/
public static void printFieldMessage(Object obj) {
Class c = obj.getClass();
/*
* Member variables are also objects
* java.lang.reflect.Field
* Field Class encapsulates operations on member variables
* getFields()Method gets all public Member variable information
* getDeclaredFields Get the information of the member variables declared by the class itself
*/
//Field[] fs = c.getFields();
Field[] fs = c.getDeclaredFields();
for (Field field : fs) {
//Get the class type of the type of the member variable
Class fieldType = field.getType();
String typeName = fieldType.getName();
//Get the name of the member variable
String fieldName = field.getName();
System.out.println(typeName+" "+fieldName);
}
}
/**
* Prints information about the constructor of the object
* @param obj
*/
public static void printConMessage(Object obj){
Class c = obj.getClass();
/*
* Constructors are also objects
* java.lang. Constructor The constructor information is encapsulated in
* getConstructors Get all public Constructor for
* getDeclaredConstructors Get all constructors
*/
//Constructor[] cs = c.getConstructors();
Constructor[] cs = c.getDeclaredConstructors();
for (Constructor constructor : cs) {
System.out.print(constructor.getName()+"(");
//Get the parameter list of the constructor -- > get the class type of the parameter list
Class[] paramTypes = constructor.getParameterTypes();
for (Class class1 : paramTypes) {
System.out.print(class1.getName()+",");
}
System.out.println(")");
}
}
}
MethodDemo:
===========
MethodDemo1.java
import java.lang.reflect.Method;
public class MethodDemo1 {
public static void main(String[] args) {
//To get print (int, int) method 1 One way to get class information is to get the class type first
A a1 = new A();
Class c = a1.getClass();
/*
* 2.Get the method name and parameter list to determine
* getMethod What you get is public Method of
* getDelcaredMethod Self declared method
*/
try {
//Method m = c.getMethod("print", new Class[]{int.class,int.class});
Method m = c.getMethod("print", int.class,int.class);
//Reflection operation of method
//a1.print(10, 20); The reflection operation of method is to use m object to make method call and A1 The effect of the print call is exactly the same
//Method returns null if there is no return value, and returns a specific return value if there is a return value
//Object o = m.invoke(a1,new Object[]{10,20});
Object o = m.invoke(a1, 10,20);
System.out.println("==================");
//Get method print(String,String)
Method m1 = c.getMethod("print",String.class,String.class);
//Reflection operation with method
//a1.print("hello", "WORLD");
o = m1.invoke(a1, "hello","WORLD");
System.out.println("===================");
// Method m2 = c.getMethod("print", new Class[]{});
Method m2 = c.getMethod("print");
// m2.invoke(a1, new Object[]{});
m2.invoke(a1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class A{
public void print(){
System.out.println("helloworld");
}
public void print(int a,int b){
System.out.println(a+b);
}
public void print(String a,String b){
System.out.println(a.toUpperCase()+","+b.toLowerCase());
}
}
MethodDemo2.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
public class MethodDemo2 {
public static void main(String[] args) {
UserService us = new UserService();
/*
* Enter commands from the keyboard to perform operations
* input update The command is called update method
* input delete The command is called delete method
* ...
*/
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Please enter a command:");
String action = br.readLine();
/*if("update".equals(action)){
us.update();
}
if("delete".equals(action)){
us.delete();
}
if("find".equals(action)){
us.find();
}*/
/*
* action It's the method name. There are no parameters--->The reflection operation through the method will be much simpler
* Pass the method object and then perform the reflection operation
*/
Class c = us.getClass();
Method m = c.getMethod(action);
m.invoke(us);
} catch (Exception e) {
e.printStackTrace();
}
}
}
MethodDemo3.java
ending
Finally, for the above content, I recommend an Android material, which should be useful to you.
The first is a list of knowledge: (technologies we need to master for Android and mobile Internet)
Generic principle, reflection principle, Java virtual machine principle, thread pool principle
Annotation principle, annotation principle, serialization
Activity knowledge system (activity life cycle, activity task stack, activity startup mode, View source code, Fragment kernel related, service principle, etc.)
Code framework structure optimization (data structure, sorting algorithm, design pattern)
APP performance optimization (user experience optimization, adaptation, code tuning)
Hot repair, hot upgrade, Hook technology, IOC architecture design
NDK (C programming, C + +, JNI, LINUX)
How to improve development efficiency?
MVC, MVP, MVVM
Wechat applet
Hybrid
Flutter
Next is the list of materials: (knock on the blackboard!!!)
The collection channel is set up for you here~
Click my GitHub for free
1. Data structure and algorithm
2. Design mode
3. A full set of systematic advanced architecture videos; Seven mainstream technology modules, video + source code + Notes
4. Interview information package (how can a comprehensive summary of interview questions be missing ~)
No matter what difficulties we encounter, they should not be the reason for us to give up! Encourage each other~
If you see here and think the article is well written, give it a compliment? If you think there is something worth improving, please leave me a message. We will carefully inquire and correct the deficiencies. thank you.
er/Android%E5%BC%80%E5%8F%91%E4%B8%8D%E4%BC%9A%E8%BF%99%E4%BA%9B%EF%BC%9F%E5%A6%82%E4%BD%95%E9%9D%A2%E8%AF%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md)**
1. Data structure and algorithm
[external chain picture transferring... (img-dCm2Itc2-1643958688248)]
2. Design mode
[external chain picture transferring... (img-C4E8HPTJ-1643958688248)]
3. A full set of systematic advanced architecture videos; Seven mainstream technology modules, video + source code + Notes
[external chain picture transferring... (IMG agapgyxw-1643958688249)]
4. Interview information package (how can a comprehensive summary of interview questions be missing ~)
[external chain picture transferring... (img-ckYvo3WY-1643958688249)]
No matter what difficulties we encounter, they should not be the reason for us to give up! Encourage each other~
If you see here and think the article is well written, give it a compliment? If you think there is something worth improving, please leave me a message. We will carefully inquire and correct the deficiencies. thank you.
[external chain picture transferring... (img-9J3TIrPW-1643958688250)]