反射入门基础

TestReflect.java

package com.huawei.reflect;

public class TestReflect {

int a =0;

private String name = "lisi";

protected long b = 2L;

public double c = 3.0d;

public static short e = 20;

static String fav = "read";


public void add(int a,int b){
System.out.println(a+b);
}
public void add(int a,String b){
System.out.println(a+b);
}

private static String say(){
return "Hello static say";
}
private String say(String msg){
return "Hello "+msg;
}

static int add(int a){
return a+100;
}

public static int add(int a,int b, int c){
return a+b+c;
}

public TestReflect() {
System.out.println("TestRefect.TestRefect()");
}
TestReflect(int a) {
System.out.println("TestRefect.TestRefect() "+a);
}

public TestReflect(long b, double c) {
super();
this.b = b;
this.c = c;
System.out.println("this is a test");
}

}

Test.java

package com.huawei.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test {
public static void testGetClass() throws ClassNotFoundException {

/**
* 得到一个类的class对象
*
* 有一下几种方式
*/

//直接通过类名.class 得到class对象
Class<?> clazz = TestReflect.class;

//通过对象.getClass() 方法得到
Class<?> clazz1 = new TestReflect().getClass();

//通过Class提供的forName方法
Class<?> clazz2 = Class.forName("com.cdsxt.reflect.TestReflect");

System.out.println(clazz);
System.out.println(clazz1);
System.out.println(clazz2);


}

/**
* 测试 得到 类里面的 属性
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static void testGetField() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
//得到 类的结构信息
Class<?> clazz = TestReflect.class;

//得到 公共的成员字段
Field c = clazz.getField("e");

TestReflect t = new TestReflect();
Object oo = new TestReflect();
Object oo1 = new TestReflect(3L,50.0d);
// t.e;
// TestReflect.e;

//oo.c
//如果 该属性是隶属于类的 则 取值的时候 不需要指定对象 直接穿null
// Object o = c.get(oo1);
Object o = c.get(null);


System.out.println(o);
//属性名
System.out.println(c.getName());
//得到修饰符
System.out.println(c.getModifiers());
//得到Field的class对象
System.out.println(c.getClass());
//得到属性字段的类型
System.out.println(c.getType());


System.out.println("--------------------");
//获取 给定类里面的 所有公有的 公共属性
Field []fields = clazz.getFields();
for(Field f:fields){
System.out.println(f.getName());
//得到修饰符
System.out.println(f.getModifiers());
//得到Field的class对象
System.out.println(f.getClass());
//得到属性字段的类型
System.out.println(f.getType());
}


//获取已有的属性字段

Field name = clazz.getDeclaredField("name");
//得到原来的状态
boolean flag = name.isAccessible();
//然后在强制打开
name.setAccessible(true);
System.out.println(name.get(oo1));
//将原来的状态设置回去
name.setAccessible(flag);
//System.out.println(name.get(oo1));
}

/**
* 测试 获取方法
*/
public static void testGetMethod() throws Exception{
Class<?> clazz = TestReflect.class;
//o.add(1,2)
//得到 一个方法的对象

Method method = clazz.getMethod("add", int.class,String.class);
//第二种写法

Method add = clazz.getMethod("add", new Class[]{int.class,int.class});
Object o = new TestReflect();
//o.add();
method.invoke(o, 1,"Hello");
add.invoke(o, new Object[]{2,10});

//获取静态方法
Method add1 = clazz.getMethod("add", new Class[]{int.class,int.class,int.class});
//通过对象调用
// Object rs = add1.invoke(o, 10,20,30);
//通过类去调用
Object rs = add1.invoke(null, 10,20,30);

System.out.println(rs);

System.out.println("----------------------");
/**
* 调用没有参数的 方法的时候 在回去的时候 可以不传
*
* 在执行的时候 也可以不传
*
* 除了public的方法或是 属性或是构造器以外的 都要注意 他们的可访问性
*
*/
Method m = clazz.getDeclaredMethod("say");
m.setAccessible(true);
Object msg = m.invoke(null);
System.out.println(msg);

}

/**
* 测试创建实例
*/
public static void testCreateInstance() throws Exception{
Class<?> clazz = TestReflect.class;
//创建实例
//如果要使用 以下的这种方式 在给定类中 必须要存在 无参构造器
Object o = clazz.newInstance();

System.out.println(o);

// Class<TTT> c = TTT.class;
// c.newInstance();

//得到特定的构造器 然后创建实例
Constructor<?> constructor = clazz.getConstructor(new Class[]{long.class,double.class});

Object o1 = constructor.newInstance(new Object[]{20L,30.0d});
System.out.println(o1);

}



public static void main(String[] args) throws Exception {
// testGetClass();
// testGetField();
// testGetMethod();

testCreateInstance();
}
}

class TTT{

public TTT(int a) {
super();
}

}

原文地址:https://www.cnblogs.com/hwgok/p/5858430.html