JavaSE——反射

1、Java反射机制概述

在这里插入图片描述在这里插入图片描述在这里插入图片描述

1、通过new的方式和反射的方式都可以调用公共的结构,开发中应该使用哪种?
答:建议使用new
2、反射机制与面型对象中的封装性是不是矛盾的?如何看待两个技术?
答:不矛盾

2、理解Class类并获取Class实例

关于java.lang.Class类的理解
1、类的加载过程:程序经过javac.exe命令以后,会生成一个或多个字节码文件(.class结尾),
接着我们使用java.exe命令对某个字节码文件进行解释运行。相当于将某个字节码文件加载到内存中,此过程称为类的加载,加载到内存中的类我们就称为运行时类,此运行时类,就成为Class的一个实例。
2、换句话说,Class的实例就对应着一个运行时例
3、加载到内存中的运行时类,会缓存一定的时间,在此时间内,我们可以通过不同的方式来获取此运行时类。

 @Test                                                                   
public void test1() throws ClassNotFoundException {                      
     //方式一:调用运行时累的属性.class                                               
     Class<Person> personClass = Person.class;                           
     System.out.println(personClass);                                    
     //方式二:通过运行时类的对象                                                     
     Person person = new Person();                                       
     Class<? extends Person> aClass = person.getClass();                 
     System.out.println(aClass);                                         
     //方式三:使用Class的静态类                                                   
     Class<?> aClass1 = Class.forName("java.lang.String");               
     System.out.println(aClass1);
     //方式三:类加载器                                                                  
     ClassLoader classLoader = test.class.getClassLoader();                      
     Class<?> aClass2 = classLoader.loadClass("java.lang.String");
     System.out.println(aClass2);                         
 }                                                                       

在这里插入图片描述数组的类型和维度相同就是同一个类

3、类的加载与ClassLoader的理解

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

//对于自定义类,使用系统类加载器进行加载
ClassLoader classLoader1 = test.class.getClassLoader();//test是public类
System.out.println(classLoader1);
//调用系统类加载的getParent()获取拓展类加载器
ClassLoader parent = classLoader1.getParent();
System.out.println(parent);
//调用拓展类加载器的getParent()无法获取引导类加载器
//引导类加载器主要负责加载java的核心类库,无法加载自定义类
ClassLoader parent1 = parent.getParent();
System.out.println(parent1);
/*
sun.misc.Launcher$AppClassLoader@18b4aac2
sun.misc.Launcher$ExtClassLoader@28a418fc
null
*/

通过类加载器获得配置文件

 @Test
 //方法1
public void test1() throws IOException {
     Properties pros = new Properties();
     //此时的文件默认为当前的module下
     FileInputStream fis = new FileInputStream("jdbc.properties");
     pros.load(fis);

     String user = pros.getProperty("user");
     String password = pros.getProperty("password");
     System.out.println(user+" "+password);
 }
 @Test
 //方法2:使用类加载器获得
public void test1() throws Exception {
     Properties pros = new Properties();
     //配置文件路径默认在src下
     ClassLoader classLoader = test.class.getClassLoader();
     InputStream ras = classLoader.getResourceAsStream("jdbc1.properties");
     pros.load(ras);

     String user = pros.getProperty("user");
     String password = pros.getProperty("password");
     System.out.println(user+" "+password);
 }

4、创建运行时类的对象

    @Test
public void test1() throws InstantiationException, IllegalAccessException {
     //通过反射创建运行时类的对象
     Class<Person> clazz = Person.class;
     //要想能够调用此方法,创建时应该有空参构造器并且空参构造器得允许访问
     //在javaBean中要求提供一个public空参构造器:
     //便于通过反射创建运行时类的对象;便于子类继承运行此类时,默认调用super()时,保证父类有此构造器
     Person person = clazz.newInstance();
     System.out.println(person);
 }

体会反射的动态性

public class test{
    @Test
   public void test1() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        for(int i=0;i<10;i++){
            int n=new Random().nextInt(2);
            switch (n){
                case 0:
                    test.fun(new String("java.util.Date"));
                    break;
                case 1:
                    test.fun(new String("java.lang.Object"));
                    break;
            }
        }
    }

    static void fun(String s) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        Class<?> name = Class.forName(s);
        Object o = name.newInstance();
        System.out.println(o.toString());
    }
}

5、获取运行时类的完整结构

  1. 获取当前运行时类的属性结构
Class clazz=Person.class;
Class[] fields=clazz.getFields()//获取当前运行类及其父类中声明的public权限的属性
Class[] fields=clazz.getDeclaredFields()//获取当前运行类中所有的属性,没有权限限制,不包括父类
//获得权限修饰符
for(Field i :fields){
    int i1 = i.getModifiers();
    System.out.println(Modifier.toString(i1));
}
//数据类型
for(Field i :fields){
    Class<?> type = i.getType();
    System.out.println(type.getName());
}
//获得变量名
for(Field i :fields){
    String name = i.getName();
    System.out.println(name);
}
  1. 获得运行时类的方法结构
//获得运行类及父类中public声明的所有方法
Class<Person> aClass = Person.class;
Method[] methods = aClass.getMethods();//getDeclaredMethods
for(Method i:methods){
    System.out.println(i);
}
//获取注解
for(Method i:methods){
    Annotation[] annotations = i.getAnnotations();
    for(Annotation j:annotations){
        System.out.println(j);//只能时生存周期时Runtime的注解才可以被获取到
    }
}
//获取权限修饰符
for(Method i:methods){
    System.out.println(Modifier.toString(i.getModifiers()));
}
//返回值类型
for(Method i:methods) {
    System.out.println(i.getReturnType());
}
//方法名
for(Method i:methods) {
    System.out.println(i.getName());
}
//获得异常
for(Method i:methods) {
    Class<?>[] exceptionTypes = i.getExceptionTypes();
    if(!(exceptionTypes==null&&exceptionTypes.length==0)){
        for(int j=0;j<exceptionTypes.length;++j){
            System.out.println(exceptionTypes[j]);
        }
    }
}
  1. 获得构造器
for (Constructor<?> constructor : aClass.getConstructors()) {
    System.out.println(constructor);
}
  1. 获取父类
Class<? super Person> superclass = aClass.getSuperclass();
System.out.println(superclass);
//带泛型的父类
Type genericSuperclass = aClass.getGenericSuperclass();
System.out.println(genericSuperclass);
//获得泛型
Class<Person> aClass = Person.class;
Type genericSuperclass = aClass.getGenericSuperclass();
ParameterizedType genericSuperclass1 = (ParameterizedType) genericSuperclass;
Type[] actualTypeArguments = genericSuperclass1.getActualTypeArguments();
for(Type i:actualTypeArguments){
    System.out.println(i);
}
  1. 获取接口、包
Class<?>[] interfaces = aClass.getInterfaces();
for(Class i:interfaces){
    System.out.println(i);
}
Package aPackage = aClass.getPackage();
System.out.println(aPackage);

6、调用运行时的指定结构

  1. 调用运行时类指定的属性
Class<Person> aClass = Person.class;
//创建运行时类的对象
Person person = aClass.newInstance();
//获取指定的属性
Field name = aClass.getDeclaredFiel("name");
//设置指定的值set(设置那个对象,设置为多少)
name.setAccessible(true);
name.set(person,"dcd");
//获取当前属性的值get(获得那个对象,值为多少)
Object o = (String)name.get(person);
System.out.println(o.toString());
  1. 调用运行时类指定的方法
Class<Person> aClass = Person.class;
//创建运行时类的对象
Person person = (Person)aClass.newInstance();
//获取指定的方法:getDeclaredMethod(方法名称,获取方法的形参列表)
Method person1 = aClass.getDeclaredMethod("fun", String.class);
person1.setAccessible(true);
//方法.invoke(方法调用对象,参数。。。),返回值为该方法的返回值
person1.invoke(person,"dcd");
//调用静态方法
person1.invoke(Person.class,"dcd");
person1.invoke(null,"dcd");
  1. 调用构造器
Class<Person> aClass = Person.class;
Constructor<Person> declaredConstructor = aClass.getDeclaredConstructor(String.class, int.class);
declaredConstructor.setAccessible(true);
Person dcd = declaredConstructor.newInstance("dcd", 20);
System.out.println(dcd);

7、反射的应用:动态代理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值