JAVA 反射 总结 之 初级 (二)
2015-05-19 22:18
162 查看
在上一节段中我们从整体上描述了反射,深入了解了Class类,有了Class对象 我们能做什么个 ,本章节做深入的探索和学习:
概述:
我们创建运行时类的对象 调用的是:
Person person = new Person(); 也是调用运行时类的空参的构造器;
第一Part: 通过反射调用类的完整结构
获取属性:
获取方法:
获取运行时类中的其他结构
@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);
}
概述:
我们创建运行时类的对象 调用的是:
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);
}
相关文章推荐
- JAVA 反射 总结 之 初级 (一)
- Java反射基础总结
- 黑马程序员_Java反射技术学习总结
- java初级面试总结
- 总结一些考察初级Java程序员技术能力的要点(转)
- JAVA 反射 总结 之 动态代理
- Java反射:边看书边总结
- java反射基础知识总结
- Java复习笔记+经验总结-06 多线程 泛型 反射
- JAVA 中从应用场景中来总结反射的使用
- Java基于自定义注释和反射机制实现初级通用DAO
- java反射的总结与疑问
- Java反射经典实例 Java Reflection Cookbook (初级)
- 基于java中反射的总结分析
- 黑马程序员——Java高新技术之反射学习总结一
- java反射学习总结
- Java反射学习总结(2)——动态加载类
- 周末总结了一份初级java开发人员笔试题
- Java基础总结从0开始(三):Java反射原理
- Java反射总结(一)