Java基础--反射

来源:互联网 发布:南京牛商网络靠谱么 编辑:程序博客网 时间:2024/06/03 18:43

反射是什么

ClassLoader的认识

通过这边精华博客来了解ClassLoader:

深入分析Java ClassLoader原理


反射涉及的对象:Class、Constructor、MethodFieldAnnotation相关泛型相关


通过代码演示反射的效果:

1.首先创建一个的接口

import java.io.Serializable;public interface MyInterface extends Serializable{}

2.创建一个注释类

import static java.lang.annotation.ElementType.CONSTRUCTOR;import static java.lang.annotation.ElementType.FIELD;import static java.lang.annotation.ElementType.LOCAL_VARIABLE;import static java.lang.annotation.ElementType.METHOD;import static java.lang.annotation.ElementType.PARAMETER;import static java.lang.annotation.ElementType.TYPE;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation {String value();}
3.创建一个继承的类

public class Creature<T> {public double weight;public void breath(){System.out.println("呼吸!");}}

4.创建一个Person对象并且实现上面的接口和继承上面类

@MyAnnotation(value="atguigu")public class Person extends Creature<String> implements Comparable,MyInterface{public String name;private int age;int id;//创建类时,尽量保留一个空参的构造器public Person() {super();}public Person(String name, int age) {super();this.name = name;this.age = age;}public Person(String name) {super();this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@MyAnnotation(value="abc123")public void show(){System.out.println("我是一个人");}public void display(String nation) throws Exception{System.out.print("我的国籍是:"+nation);}@Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + "]";}@Overridepublic int compareTo(Object o) {return 0;}class Bird{}}

5.1测试构造器对象

import java.lang.reflect.Constructor;import org.junit.Test;public class TestConstructor {@Testpublic void test1() throws Exception{String className="com.atguigu.java.Person";Class clazz=Class.forName(className);//创建对应的运行时类的对象//要想能够创建成功://1.要求对应的运行时类要有空参的构造器//2.构造器的权限修饰符的权限要足够Person p=(Person) clazz.newInstance();System.out.println(p);}@Testpublic void test2(){Class clazz=Person.class;Constructor[] cons=clazz.getDeclaredConstructors();for(Constructor c:cons){System.out.println(c);}}}
5.2.测试效果:

test1的运行效果:

Person [name=null, age=0]

test2的运行效果:
public com.atguigu.java.Person()
public com.atguigu.java.Person(java.lang.String,int)
public com.atguigu.java.Person(java.lang.String)


6.1测试"方法"

import java.lang.reflect.Field;import java.lang.reflect.Modifier;import org.junit.Test;public class TestField {@Testpublic void test1(){Class clazz=Person.class;//1.getFields():只能获取到运行时类中及其父类中声明为public的属性Field[] fields=clazz.getFields();for(Field field:fields){System.out.println(field);}//2.Field[] fields1=clazz.getDeclaredFields();for(Field field:fields1){System.out.println(field);}}/** * 权限修饰符 变量类型 变量名 * 获取属性的各个部分的内容 */@Testpublic void test2(){Class clazz=Person.class;Field[] fields=clazz.getDeclaredFields();for(Field f:fields){//1.获取每个属性的权限符String str=Modifier.toString(f.getModifiers());System.out.print(str +" ");//2.获取属性的类型Class type=f.getType();System.out.print(type.getName()+" ");//3.获取属性名System.out.println(f.getName());}}}

6.2.测试效果:

test1的运行效果:

public java.lang.String com.atguigu.java.Person.name
public double com.atguigu.java.Creature.weight
public java.lang.String com.atguigu.java.Person.name
private int com.atguigu.java.Person.age
int com.atguigu.java.Person.id

test2的运行效果:
public java.lang.String name
private int age
 int id

7.1测试其他方面:

import java.lang.annotation.Annotation;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import org.junit.Test;public class TestOthers {@Testpublic void test1() {Class clazz = Person.class;// 获取运行时的父类Class superClass = clazz.getSuperclass();System.out.println(superClass);//打印带有泛型的父类Type type = clazz.getGenericSuperclass();System.out.println(type);//获取实现的接口Class[] interfaces=clazz.getInterfaces();for(Class i:interfaces){System.out.println(i);}//获取所在的包Package p=clazz.getPackage();System.out.println(p);//获取注解Annotation[] anns=clazz.getAnnotations();for(Annotation a:anns){System.out.println(a);}}//获取父类的泛型@Testpublic void test2() {Class clazz = Person.class;Type type=clazz.getGenericSuperclass();ParameterizedType param=(ParameterizedType) type;//通过向下转化为Class Type[] ars=param.getActualTypeArguments(); System.out.println(((Class)ars[0]).getName());}}
7.2.测试效果:

test1的运行效果:

class com.atguigu.java.Creature
com.atguigu.java.Creature<java.lang.String>
interface java.lang.Comparable
interface com.atguigu.java.MyInterface
package com.atguigu.java
@com.atguigu.java.MyAnnotation(value=atguigu)

test2的运行效果:
java.lang.String







0 0
原创粉丝点击