反射的简单的例子练习
package reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 反射的学习
* @author hjsjy
*
*/
public class Test01 {
public int a;
private int b;
private String cString;
public String name;
protected String dString;
int d;
public Test01() {
System.out.println("构造方法");
}
private Test01(int a) {
System.out.println("私有的构造方法"+a);
}
public Test01(int a,int b) {
System.out.println("构造方法两个参数");
}
//**************成员方法***************//
public void show1(String s){
System.out.println("调用了:公有的,String参数的show1(): s = " + s);
}
protected void show2(){
System.out.println("调用了:受保护的,无参的show2()");
}
void show3(){
System.out.println("调用了:默认的,无参的show3()");
}
private String show4(int age){
System.out.println("调用了,私有的,并且有返回值的,int参数的show4(): age = " + age);
return "abcd";
}
@Override
public String toString() {
return "Test01 [a=" + a + ", b=" + b + ", cString=" + cString + ", name=" + name + ", dString=" + dString
+ ", d=" + d + "]";
}
public static void main(String[] args) throws Exception{
Class<?> class1 = Class.forName("reflect.Test01");
System.out.println("------------------获取构造方法并使用-----------------------------------");
Constructor<?>[] constructors = class1.getConstructors();
for(Constructor aConstructor :constructors) {
System.out.println(aConstructor);
}
System.out.println("---------------------------------------------------------------------------");
constructors = class1.getDeclaredConstructors();
for(Constructor constructor: constructors) {
System.out.println(constructor);
}
System.out.println("---------------------------------------------------------------------------");
Constructor<?> declaredConstructor = class1.getDeclaredConstructor(int.class);
System.out.println(declaredConstructor);
declaredConstructor.setAccessible(true);
Object newInstance = declaredConstructor.newInstance(1);
System.out.println("--------------------获取成员变量并调用----------------------------------------");
Field[] fields = class1.getFields();
for(Field f:fields) {
System.out.println(f);
}
System.out.println("---------------------------------------------------------------------------");
Field[] declaredFields = class1.getDeclaredFields();
for(Field field :declaredFields) {
System.out.println(field);
}
Field field = class1.getField("name");
System.out.println(field);
Object newInstance2 = class1.getConstructor().newInstance();
field.set(newInstance2, "hjsjy");
Test01 test01=(Test01)newInstance2;
System.out.println(test01.name);
System.out.println("---------------------------------------------------------------------------");
Field declaredField = class1.getDeclaredField("b");
System.out.println(declaredField);
declaredField.setAccessible(true);
declaredField.set(newInstance2, 188);
System.out.println(newInstance2);
System.out.println("-------------------------获取方法并掉调用-----------------------------------");
Method[] methods = class1.getMethods();
for(Method method:methods) {
System.out.println(method);
}
Method[] declaredMethods = class1.getDeclaredMethods();
for(Method method: declaredMethods) {
System.out.println(method);
}
Method method = class1.getMethod("show1", String.class);
System.out.println(method);
Object newInstance3 = class1.getConstructor().newInstance();
method.invoke(newInstance3, "hjsjy");
Method declaredMethod = class1.getDeclaredMethod("show4", int.class);
System.out.println(declaredMethod);
declaredMethod.setAccessible(true);
Object invoke = declaredMethod.invoke(newInstance3, 500);
System.out.println(invoke);
}
}
输出结果
------------------获取构造方法并使用-----------------------------------
public reflect.Test01(int,int)
public reflect.Test01()
---------------------------------------------------------------------------
public reflect.Test01(int,int)
private reflect.Test01(int)
public reflect.Test01()
---------------------------------------------------------------------------
private reflect.Test01(int)
私有的构造方法1
--------------------获取成员变量并调用----------------------------------------
public int reflect.Test01.a
public java.lang.String reflect.Test01.name
---------------------------------------------------------------------------
public int reflect.Test01.a
private int reflect.Test01.b
private java.lang.String reflect.Test01.cString
public java.lang.String reflect.Test01.name
protected java.lang.String reflect.Test01.dString
int reflect.Test01.d
public java.lang.String reflect.Test01.name
构造方法
hjsjy
---------------------------------------------------------------------------
private int reflect.Test01.b
Test01 [a=0, b=188, cString=null, name=hjsjy, dString=null, d=0]
-------------------------获取方法并掉调用-----------------------------------
public static void reflect.Test01.main(java.lang.String[]) throws java.lang.Exception
public java.lang.String reflect.Test01.toString()
public void reflect.Test01.show1(java.lang.String)
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
public static void reflect.Test01.main(java.lang.String[]) throws java.lang.Exception
public java.lang.String reflect.Test01.toString()
public void reflect.Test01.show1(java.lang.String)
protected void reflect.Test01.show2()
void reflect.Test01.show3()
private java.lang.String reflect.Test01.show4(int)
public void reflect.Test01.show1(java.lang.String)
构造方法
调用了:公有的,String参数的show1(): s = hjsjy
private java.lang.String reflect.Test01.show4(int)
调用了,私有的,并且有返回值的,int参数的show4(): age = 500
abcd
本文参考:https://blog.csdn.net/sinat_38259539/article/details/71799078?tdsourcetag=s_pcqq_aiomsg