反射

java.lang.Class:是反射机制的源头
我们创建了一个类,通过编译(javac。exe)生成对应的。class文件 之后我们使用java.exe加载(JVM的类加载完成的)
class文件,此。class文件加载到内存以后,就是一个运行实类,存在缓存区中,那么这个运行就是一个Class的实例

1.每运行时类只加载一次1

2.有了Class的实例以后,我们才能进行如下的操作

    1)创建对应的运行时类的对象
    2)获取对应的运行时类的完整结构(属性,方法,构造器,内部类,父类。所在的包。异常,注解..)
    3.)调用对应的运行实类的指定结构(属性,方法,构造器)
    4.)放射的应用,动态代理。

如何获取Class的实例(3)
1.调用运行是类本身的。class属性
Class class1=Pseson.class;
System.out.println(class1.getName());
2.通过运行时类的对象获取
Person p=new Person();
Class class1=p.getClass();
System.out.println(class2.getName());
3.通过Class的静态方法获取 通过此方式,体会一下,反射的动态性。
String className=”com.atguigu.java.Person”;
Class class4=className.forName(className);
System.out.println(class4.getName());

4.(了解)通过类的加载器

    ClassLoader classLoader=this.getClass().getClassLoader();
    Class class5=classLoader.loaderClass(className);
    System.out.println(class5.getName());

//获取对应的运行时类的属性
Class clazz=Person.class;
//1.getFileds(),只能获取运行时类中及父类中声明为public的属性
Filed[] fileds=clazz.getFileds();
for(int i=0;i,fileds.length;i++){
    System.out.print(fileds[i]);
}
//2.getDeclaredFileds(),获取运行时类本身声明的所有属性。
Filed[] fileds=clazz.getDeclaredFileds();
for(Filed f: fileds){
    System.out.print(f.getName());
}
//权限修饰符 变量类型 变量名
//获取属性的各个部分的内容
Filed[] fileds=clazz.getDeclaredFileds();
for(Filed f: fileds){
    int i=f.getModifiers();
    String str=Modifier.toString(i);
    System.out.print(str+" ");
    //获取属性名
    Class type=f.getType();
    System.out.print(type.getName());
    //获取名
    System.out.print(f.getName())
}

//1.获取运行时类的方法
//1.getMethods();获取运行时类及父类中所有的声明为public的方法。
Method[] m1=clazz.getMethods();
for(Method m : m1){
    System.out.printLn(m);
}
//2.getDeclaredMethods();获取运行时类本身的所有方法
Method[] m1=clazz.getDeclaredMethods();
for(Method m : m1){
    System.out.printLn(m);
}
//注解 权限修饰符 返回值类型 方法名 形参类表 异常 
Class clazz=Person.class;
Method[] m1=clazz.getMethods();
for(Method m : m1){
    //1.注解
    Annotation[] ann=m.getAnnotations();
    for(Annotation a :ann){
        System.out.println(a);
    }
    //2.权限修饰符
    String str=Modifier.toString(m.getModifiers());
    System.out.print(str +" ");
    //3.返回值类型
    Class returnType=m.getReturnType();
    System.out.print(returnType.getName()+" ");
    //方法名
    System.out.print(m.getName()+" ");
    //形参类表
    System.out.print("(");
    Class[] params=m.getParameterTypes();
    for(int i=0;i<params.length;i++){
        System.out..print(params[i].getName()+" args-"+i+" ");
    }
    System.out.print(")");
    //6.异常
    Class[] exps=m.getExceptionTypes();
    if(exps.length !=0){
        System.out.print("throw");
    }
    for(int i=0; i<exps.length;i++){
        System.out.print(exps[i].getName()+" ");
    }
    system.o.printLn();
}
//获取对应的运行时类的构造器
String className="com.atugit.java.Person";
Class clazz=Class.forName(className);
Constructor[] cons=clazz.getDeclaredConstructors();//其获取的其他的注解 权限修饰符 
返回值类型 方法名 形参类表 异常 与前面方法类似。
for(Constructor c: cons){
    System.out.println(c);
}

//1.获取运行时类的父类
Class clazz=Person.class;
Class superClass=clazz.getSuperclass();
System.out.println(superClass);
//获取带泛型的父类
Class clazz=Person.class;
Type type1=clazz.getGenericSuperclass();
System.out.println(type1);
//3.*获取父类的泛型
Class clazz=Person.class;
Type type1=clazz.getGenericSuperclass();
ParameterizedType param=(ParameterizedType)Type1;
Type[] ars=param.getActualTypeArguments();
System.out.println(((Class)ars[0]).getName());
//4.获取实现的接口
Class clazz=Person.class;
Class[] interface=clazz.getInterfaces();
for(Class i : interface){
    System.out.println(i);
    }
//5.获取所在的包
Class clazz=Person.class;
Package pack=clazz.getPackage();
System.out.println(pack);
//6.获取注解
Class clazz=Person.class;//只有注解是被RUNTIME修饰的才能实现
Annotation[] anns=clazz.getAnnotation();
for(Annotation a: anns){
    System.out.println(a);
    }
//7.获取内部类直接getClass就行

//获取运行时类中指定的属性
Class clazz=Person.class;
//1.获取指定的属性
//getFiled(String,filedName):获取运行时类中声明为public的指定的属性名为 filedName的属性
Filed name=clazz.getFiled("name");
//2.创建运行时类的对象
Person p=(Person)clazz.newInstance();
System.out.println(p);
//3.将运行时类的指定属性赋值
name.set(p,"jeer");
System.out.println(p);
//getDeclaredFiled(String,filedName):获取运行时类中指定的名为fileName的属性
Filed age=clazz.getDeclaredFiled("age");
//由于权限修饰符的限制,为了保证可以给属性赋值,需要在操作修饰符前使得此属性可被操作;
age.setAccessible(true);
age.set(p,10);
System.out.println(p);

//调用运行时类中的指定方法
Class clazz=Person.class;
//getMethod(String methodName,Class ..params):获取运行时类中声明为public的指定的方法
Method m1=clazz.getMethod("show");
Person p=(Person)clazz.interface();
//调用指定的方法 Object obj,Object ...obj)
Object returnVal=m1.invoke(p);//输出show方法中的System.out.print的语句
System.out.println(returnVal)//一个值。

//对于运行时类静态的方法的调用
Method m3=clazz.getMethod("info");
m3.invoke(Person.class);
//getDeclaredMethod(String methodName,Class ...params):获取时运行时类中声明了的指定的方法
Method m4=clazz.getDeclaredMethod("display",String.class,Integer.class);
m4.setAccessible(true);
Object value=m4.invoke(p,"chin",10);
System.out.println(value);

//调用指定的构造器,创建运行实类
String className="com.atugit.java.Person";
Class clazz=Class.filedName(className);
Constructor cons=clazz.getDeclaredConstructor(String.class,int.class);
cons.setAccessible(true);
Person p=(Person)cons.newInstance("luo",10);
System.out.println(p);

//

如何创建Class的实例(重点)

1.1过程:源文经过编译(javac.exe)以后,得到了一个或多个.class文件, .class文件, .class文件经过运行(java.exe)
这步就需要运行类的加载(通过JVM的类加载器),记载到内存中的缓存,每一个放入中的 .class文件就是一个Class实例。
1.2Class的一个对象,对应着一个运行时类,相当于一个运行时类本身充当了Class的一个实例。
1.3java.lang.Class是反射的源头。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值