您的位置:首页 > 编程语言 > Java开发

JAVA 反射 总结 之 初级 (二)

2015-05-19 22:18 162 查看
在上一节段中我们从整体上描述了反射,深入了解了Class类,有了Class对象  我们能做什么个 ,本章节做深入的探索和学习:

概述:

我们创建运行时类的对象 调用的是:

Person person = clazz.newInstance();
创建对应的运行时类对象,使用newInstance(),实际上就是调用了运行时类的空参的构造器,当然你要调用空参的构造器运行类就要有一个空参的构造器,且权限足够;

Person person = new Person(); 也是调用运行时类的空参的构造器;

第一Part: 通过反射调用类的完整结构

获取属性:

@Test
public void test() throws InstantiationException, IllegalAccessException
{
Class<Person> clazz = Person.class;
// 只能获取运行时类中及其父类中声明为public的变量
Field[] fieldArry = clazz.getFields();
for (Field field : fieldArry)
{
// 1:获取每个属性的权限修饰符
int temp = field.getModifiers();
String modify = Modifier.toString(temp);
System.out.print(modify + " ");
// 2:获取属性类型
Class type = field.getType();
System.out.print(type.getName() + " ");
// 3:获取属性名
System.out.print(field.getName());
System.out.println();
}
// 能获取运行时类中本身声明的所有属性
Field[] fieldArry01 = clazz.getDeclaredFields();
for (Field field01 : fieldArry01)
{
// 1:获取每个属性的权限修饰符
int temp = field01.getModifiers();
String modify = Modifier.toString(temp);
System.out.print(modify + " ");
// 2:获取属性类型
Class type = field01.getType();
System.out.print(type.getName() + " ");
// 3:获取属性名
System.out.print(field01.getName());
System.out.println();
}
}


获取方法:

@Test
public void test01()
{
Class<Person> clazz = Person.class;
// 获取运行时类及其父类中所有生命public的方法,object也会包含进来
Method[] methods = clazz.getMethods();
for (Method method : methods)
{
}
// 获取运行时类中的声明的所有方法
Method[] methods00 = clazz.getDeclaredMethods();
for (Method method : methods00)
{
// 获取方法的注解
Annotation[] ann = method.getAnnotations();
System.out.println(ann);
for (Annotation a : ann)
{
System.out.println(a);
}

// 获取修饰符
String str = Modifier.toString(method.getModifiers());
System.out.println(str);

// 获取返回值类型
Class returnType = method.getReturnType();
System.out.println(returnType.getName() + " ");

// 获取方法名
System.out.print(method.getName() + " ");

// 获取形参列表
System.out.print("(");
Class[] params = method.getParameterTypes();
for (Class p : params)
{
System.out.print(p.getName() + "args-" + " ");
}
System.out.println(")");

// 获取异常类型
Class[] exces = method.getExceptionTypes();
if (0 != exces.length)
{
System.out.print("throws");
}
for (Class exce : exces)
{
System.out.print(exce.getName());
}
System.out.println();
}
}

获取运行时类中的其他结构

@Test
public void test03() throws ClassNotFoundException
{
// 获取构造方法
String className = "com.reflect.Person";
Class clazz = Class.forName(className);
Constructor[] cons = clazz.getDeclaredConstructors();
for (Constructor con : cons)
{
System.out.println(con);
}
}

@Test
public void test04()
{
// 获取父类
Class clazz = Person.class;
Class superClass = clazz.getSuperclass();
System.out.println(superClass);
}

@Test
public void test05()
{
// 获取带泛型的父类
Class clazz = Person.class;
Type type = clazz.getGenericSuperclass();
System.out.println(type);
}

@Test
public void test06()
{
// 获取泛型的父类 该处在ddbc中应用较多,可以获取dao操作的哪个类,然后就可以构造相关类的对象
Class clazz = Person.class;
Type type = clazz.getGenericSuperclass();
ParameterizedType param = (ParameterizedType) type;
Type[] ars = param.getActualTypeArguments();
System.out.println(((Class) ars[0]).getName());
}

@Test
public void test07()
{
// 获取运行时类的接口
Class clazz = Person.class;
Class[] clazzs = clazz.getInterfaces();
for (Class claz : clazzs)
{
System.out.println(claz);
}
}

@Test
public void test08()
{
// 获取运行时类所在的包
Class clazz = Person.class;
Package myPackage = clazz.getPackage();
System.out.println(myPackage.getName());
}

@Test
public void test09()
{
// 获取注解 只能获取运行时类的注解@Retention(RetentionPolicy.RUNTIME)
Class clazz = Person.class;
Annotation[] annotation = clazz.getAnnotations();
for (Annotation a : annotation)
{
System.out.println(a);
}
}


第二Part
通过反射调用类中的指定方法指定属性以及构造方法

// 设置指定类中的属性
@Test
public void test10() throws Exception
{
Class<Person> clazz = Person.class;
Person person = clazz.newInstance();
// 获取运行时类的属性,并且只能获取到public修饰的属性
Field age = clazz.getField("age");
// 将运行时类的属性赋值
age.set(person, 1);
System.out.println(person);

// 该方法可获取运行时类的所有属性 包括私有
Field name = clazz.getDeclaredField("name");
// 由于属性修饰符的限制,为了保证可以给属性赋值,需要在操作此属性前设置可操作
name.setAccessible(true);
name.set(person, "Test");
System.out.println(person);
}

// 调用指定类中的方法并调用
@Test
public void test11() throws Exception
{
// 调用方法为public 有返回值的方法
Class<Person> clazz = Person.class;
Person person = clazz.newInstance();
Method method00 = clazz.getMethod("toString");
Object obj = method00.invoke(person);// 方法的返回值是object
System.out.println(obj);
// 调用静态方法
Method method01 = clazz.getMethod("display");
method01.invoke(Person.class);
// 调用private 并有两个参数 和 返回值
Method method02 = clazz.getDeclaredMethod("info", Integer.class,
String.class);
method02.setAccessible(true);
Object obj00 = method02.invoke(person, 1, "Test");
System.out.println(obj00);
}

// 调用指定的构造器
@Test
public void test12() throws Exception
{
String className = "com.reflect.Person";
Class clazz = Class.forName(className);
Constructor con = clazz.getDeclaredConstructor(int.class, String.class);
con.setAccessible(true);
Person p = (Person) con.newInstance(1, "1");
System.out.println(p);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: