- 所谓的反射实际上是通过clazz.getClass()方法获取一个类型为Class类型的对象,通过getName()、getPackage()、getMethods()等方法获取类的相关数据;
- 获取类的反射对象
1.Class.forName(全类名)–一般用这个
2.类名.class
3.对象.getClass()方法
Example2 example2 = new Example2();
Class a1 = Class.forName("Example2");
System.out.println(a1);
Class a = Example2.class;
System.out.println(a);
Class<? extends Example2> exampleC = example2.getClass();
System.out.println(exampleC);
class Example2
class Example2
class Example2
clazz.getName();
clazz.getPackage;
clazz.getInterfaces();
clazz.getSuperclass();
clazz.getMethod();
clazz.getMethods(String name, 参数);
clazz.getDeclaredMethod(String name, 参数)
clazz.getDeclaredMethods();
clazz.getConstructor(String name, 参数);
clazz.getConstructors();
获取单个(有参根据传入的是什么类型找对应的构造函数)
clazz.geDeclaredtConstructor(String name, 参数);
clazz.getDeclaredConstructors();
clazz.getField(String name);
clazz.getFields();
clazz.getDeclaredField(String name);
clazz.getDeclaredFields();
clazz.getClasses()
clazz.getDeclaredClasses()
public class Main1 {
public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, InstantiationException {
Example1 example1 = new Example1("10", "20", "30");
Class<? extends Example1> exampleC = example1.getClass();
Constructor[] constructors = exampleC.getDeclaredConstructors();
for (Constructor constructor: constructors){
System.out.println("constructor: " + constructor);
Class[] types = constructor.getParameterTypes();
for (Class type: types){
System.out.println("type: " + type);
}
Class[] exceptions = constructor.getExceptionTypes();
for (Class exception: exceptions){
System.out.println("exception: " + exception);
}
Example1 example2 = (Example1) constructor.newInstance("10", "20");
}
}
}
public class Example1 {
private String s;
private int i1, i2, i3;
public Example1(){
}
public Example1(String s, int i){
this.s = s;
this.i1 = i;
}
public Example1(String... args){
if (1 < args.length){
this.i1 = Integer.valueOf(args[0]);
}
if (2 < args.length){
this.i2 = Integer.valueOf(args[1]);
}
if (13 < args.length){
this.i3 = Integer.valueOf(args[2]);
}
}
public void print(){
System.out.println("s" + s);
System.out.println("i1" + i1);
System.out.println("i2" + i2);
System.out.println("i3" + i3);
}
}
public class Main2 {
public static void main(String[] args) throws IllegalAccessException {
Example2 example2 = new Example2();
Class<? extends Example2> exampleC = example2.getClass();
Field[] fields1 = exampleC.getFields();
for (Field field: fields1){
System.out.println("field1的名称" + field.getName());
System.out.println("field1的类型" + field.getType());
}
Field[] fields2 = exampleC.getDeclaredFields();
for (Field field: fields2){
System.out.println("field2的名称" + field.getName());
System.out.println("field2的类型" + field.getType());
field.setAccessible(true);
if (field.getType() == int.class){
field.setInt(example2, 1);
}
}
}
}
public class Example2 {
private int i;
public float f;
protected boolean b;
private String s;
}
public class Main3 {
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
Example3 example3 = new Example3();
Class<? extends Example3> clazz = example3.getClass();
Method[] methods = clazz.getDeclaredMethods();
for (Method method: methods){
System.out.println("getName: " + method.getName());
System.out.println("getReturnType: " + method.getReturnType());
System.out.println("getParameterTypes: " + method.getParameterTypes());
System.out.println("isVarArgs: " + method.isVarArgs());
if (method.getName().equals("staticMethod")){
method.invoke(example3);
}
if (method.getName().equals("publicMethod")){
method.invoke(example3 ,1);
}
}
}
}
public class Example3 {
static void staticMethod(){
System.out.println("执行static方法");
}
public int publicMethod(int i){
System.out.println("执行public方法");
return i * 100;
}
protected int protectedMethod(String s, int i){
System.out.println("执行protected方法");
return Integer.valueOf(s) + i;
}
private String privateMethod(String... strings){
System.out.println("执行private方法");
StringBuffer stringBuffer = new StringBuffer();
for (String string: strings){
stringBuffer.append(string);
}
return stringBuffer.toString();
}
}