package com.shengsiyuan.reflect;import java.lang.reflect.Array;public class ArrayTester1 { public static void main(String[] args) throws Exception { Class<?> classType = Class.forName("java.lang.String");
Object array = Array.newInstance(classType, 10);
Array.set(array, 5, "hello");
String str = (String)Array.get(array, 5);
System.out.println(str); } }
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy;@Retention(value = RetentionPolicy.RUNTIME) public @interface Value { String[] value(); } public class Test01 { //私有属性 private int a = 0; //私有方法 @Value(value = { "hello", "world" }) private int add(int b) { return a + b; } }import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method;public class Test02 { public static void main(String[] args) throws Exception { // Class cls = Test01.class; 或者 Test01 tst = new Test01(); Class cls = Class.forName("Test01"); // 反射设置属性 Field field = cls.getDeclaredField("a"); field.setAccessible(true); //更改为可访问 field.setInt(tst, 10); // 反射调用方法 Method method = cls.getDeclaredMethod("add", int.class); method.setAccessible(true); //更改为可访问 int rst = (Integer) method.invoke(tst, 8); System.out.println(rst); // 反射取得方法的注解 Annotation[] anns = method.getAnnotations(); for (Annotation a : anns) { if (a instanceof Value) { String[] strs = ((Value) a).value(); System.out.println(strs[0] + "$" + strs[1]); } } } }
class People{ //属性 //方法 } public class Test{ public void Reflect1(){ Class clazz; try { //反射机制 clazz=Class.forName("People"); People p=(People)clazz.newInstance(); } catch (Exception e) { e.printStackTrace(); } } }
{
public static void main(String[] args) throws Exception
{
Class<?> classType = Class.forName("java.lang.String");
Object array = Array.newInstance(classType, 10);
Array.set(array, 5, "hello");
String str = (String)Array.get(array, 5);
System.out.println(str);
}
}
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;@Retention(value = RetentionPolicy.RUNTIME)
public @interface Value
{
String[] value();
}
public class Test01
{
//私有属性
private int a = 0; //私有方法
@Value(value = { "hello", "world" })
private int add(int b)
{
return a + b;
}
}import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;public class Test02
{
public static void main(String[] args) throws Exception
{
// Class cls = Test01.class; 或者
Test01 tst = new Test01();
Class cls = Class.forName("Test01"); // 反射设置属性
Field field = cls.getDeclaredField("a");
field.setAccessible(true); //更改为可访问
field.setInt(tst, 10); // 反射调用方法
Method method = cls.getDeclaredMethod("add", int.class);
method.setAccessible(true); //更改为可访问
int rst = (Integer) method.invoke(tst, 8);
System.out.println(rst); // 反射取得方法的注解
Annotation[] anns = method.getAnnotations();
for (Annotation a : anns)
{
if (a instanceof Value)
{
String[] strs = ((Value) a).value();
System.out.println(strs[0] + "$" + strs[1]);
}
} }
}
//属性
//方法
}
public class Test{
public void Reflect1(){
Class clazz;
try {
//反射机制
clazz=Class.forName("People");
People p=(People)clazz.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
}