栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA学习第18天;设计模式;反射

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA学习第18天;设计模式;反射

设计模式

方案1饿汉式
方案2:在1饿汉式基础上在做了扩展,让我们有些并不需要第一时间加载出来的资源去延迟到需要时在进行加载

//创建类
class MySingle2{

    private MySingle2(){ }
    //3创建的是本类对象对应类型的引用类型变量,用来保存对象的地址值
    //引用类型的成员变量默认值是null
  private static MySingle2 single2;

    public static MySingle2 getSingle2(){
      //  synchronized (MySingle2.class)
        if (single2==null) { //判断他的结果是否为默认值 ,不是默认值说明创建过了,直接返回即可
            single2 = new MySingle2();
        }
        return single2;
    }

 }
反射

反射很强,我们可以通过反射类获取目标类中的资源,甚至是私有资源
还可以通过反射使用资源,创建对象

反射分为两种使用方式
反射和暴力反射

反射

创建字节码对象的三种方式

Class clazz  = new Teacher().getClass();
        Class clazz1 = Teacher.class;
        Class clazz2 = Class.forName("cn.tedu.exercise.Teacher");

@Test
//通过字节码对象获取`普通属性  注意 被获取的属性要被public 修饰 ,暴力反射可直接调用
    public void getgrazz3(){  
        //创建字节码对象
        Class clazz = Teacher.class;
        Field[] fields = clazz.getFields();
        System.out.println(Arrays.toString(fields));
//        clazz.getField()
        for (Field f:fields){
            System.out.println(f.getName());
            System.out.println(f.getType());
        }

 @Test
    public void getgrazz2(){    //获取普通方法
        //创建字节码对象
        Class clazz = new Teacher().getClass();
        //获取指定的方法
        Method[] method = clazz.getMethods();
        //System.out.println(Arrays.toString(method));
        for (Method m:method){
            System.out.println(m.getName());
            System.out.println(Arrays.toString(m.getParameterTypes()));
        }
    }

 @Test
    public void geltgrazz1() throws Exception {       //获取构造方法   
        //创建字节码对象
        Class clazz = Teacher.class;
        Constructor[] c1 = clazz.getConstructors();
        System.out.println(Arrays.toString(c1));
        Constructor c = clazz.getConstructor(int.class, String.class);
        System.out.println(c);
        Object o1 = c.newInstance(6, "芜湖~");
        System.out.println(o1);//重写后 是类型 属性 属性值


 }
//暴力反射

//获取私有方法
 @Test
    public void getfile1() throws Exception {
        //创建字节码对象
        Class claaa = Student.class;
        //获取类的私有方法
        Method method3 = claaa.getDeclaredMethod("eat", String.class);
        System.out.println(method3);
        //方法只能通过对象调用  ,在没有对象的情况下,创建对象,
        Object o1 = claaa.newInstance();

        //赋给方法权限

        method3.setAccessible(true);
        method3.invoke(o1 ,"歌之洋");

    }
//获取私有属性
@Test
    public void getfile2() throws Exception {
        //获取字节码对象
        Class clazz =Student.class;
        //获取私有属性
        Field field = clazz.getDeclaredField("age");
        //打印查看
        System.out.println(field);
        System.out.println(field.getType());
        System.out.println(field.getType().getName());
        //修改私有属性
        //创建对象
        Object o1 = clazz.newInstance();
        //赋予权限
        field.setAccessible(true);
        //根据对象名,修改私有属性值
        field.set(o1,9);
        //o1是指具体查看哪个对象的属性值
            System.out.println(field.get(o1));
 
    }

@Test
    public void getfile3() throws  Exception {
        //创建字节码对象
        Class clazz = Class.forName("cn.tedu.exercise.Student");
        //获取我的私有方法
        Method[] methods = clazz.getDeclaredMethods();
        System.out.println(Arrays.toString(methods));
        //创建对象
        Object o = clazz.newInstance();
        //获取权限
        for (Method m:methods){
            System.out.println(m.getName());
            System.out.println(Arrays.toString(m.getParameterTypes()));
        }
 
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678617.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号