框架核心-反射、代理、泛型

Mybatis讲义

复习反射

  • Class 和 class(关键字)
    • class 关键字,定义一个类
    • Class 普通类(也不普通)
      • 每个类都有一个字节码文件 .class文件,虚拟机要使用.class文件,必须将他放到内存

      • 可以认为每个.class的内容是放在内存中的Class对象中的(理解,重要)

      • 只要获取到Class对象,通过反射就可以完全获取类的信息

      • 获取Class的三种方式

        1. Class<?> 变量= 对象. getClass()
        2. Class<?> 变量= 类名.class
        3. Class<?> 变量=Class.forName(“全限定的类名”)

        ?表示的是等号右边可以是任意数据类型。

      • 拿到Class对象之后,就可以获取该类中的所有东西

        • 获取构造函数
        • 获取字段
        • 获取函数
        • 获取注解

复习代理

  • 使用接口生成代理对象的方式
-------1.定义一个接口--------------------
public interface ISession {
    void insert(String sql);
}
--------2.定义一个代理类----------------------
public class ProxyObject implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("执行了...");
        //因为我们此处就一处使用了代理,且传进了参数。
        System.out.println(args[0]);


        return null;
    }
}

--------3.对ISeesion接口进行代理----------------
    @Test
    public void testProxyInterface(){
//        ProxyObject px = new ProxyObject();

        ISession session = (ISession)Proxy.newProxyInstance(ISession.class.getClassLoader(),
                //该方式生成的对象无法强转为接口
//                ISession.class.getClasses(), //错误 com.sun.proxy.$Proxy4 cannot be cast to com.hdeasy.ISession
                    new Class<?>[]{ISession.class},
                    new ProxyObject()
                );
        session.insert("insert into user (1,2,3)");
    }



接口不能完成的事情,让代理帮忙完成。

泛型

//定义
public class Generic<T>{ 
    //key这个成员变量的类型为T,T的类型由外部指定  
    private T key;

    public Generic(T key) { //泛型构造方法形参key的类型也为T,T的类型由外部指定
        this.key = key;
    }

    public T getKey(){ //泛型方法getKey的返回值类型为T,T的类型由外部指定
        return key;
    }
}   


//使用
Generic<Integer> genericInteger = new Generic<Integer>(123456); 
//相当于你做了一个类
public class Generic{ 
    //key这个成员变量的类型为T,T的类型由外部指定  
    private Integer key;

    public Generic(Integer key) { //泛型构造方法形参key的类型也为T,T的类型由外部指定
        this.key = key;
    }

    public Integer getKey(){ //泛型方法getKey的返回值类型为T,T的类型由外部指定
        return key;
    }
}   

public class Generic{ 
    //key这个成员变量的类型为T,T的类型由外部指定  
    private Integer key;

    public Generic(Integer key) { //泛型构造方法形参key的类型也为T,T的类型由外部指定
        this.key = key;
    }

    public Integer getKey(){ //泛型方法getKey的返回值类型为T,T的类型由外部指定
        return key;
    }
}   

Generic<String> genericInteger = new Generic<String>("123456"); 
//相当于
public class Generic{ 
    //key这个成员变量的类型为T,T的类型由外部指定  
    private String key;

    public Generic(String key) { //泛型构造方法形参key的类型也为T,T的类型由外部指定
        this.key = key;
    }

    public String getKey(){ //泛型方法getKey的返回值类型为T,T的类型由外部指定
        return key;
    }
}   

? 和 T

  • Class<?> 和 Class

Class是在定义的时候,Class<?>是在使用的时候

在这里插入图片描述

在这里插入图片描述

  • 泛型类型是可以限定的

    • public class Generic
      //这一行代码也会报错,因为String不是Number的子类
      Generic generic1 = new Generic(“11111”)
  • List list;

  • 泛型修饰方法

        //我想说的其实是这个,虽然在方法中使用了泛型,但是这并不是一个泛型方法。
        //这只是类中一个普通的成员方法,只不过他的返回值是在声明泛型类已经声明过的泛型。
        //所以在这个方法中才可以继续使用 T 这个泛型。
        public T getKey(){
            return key;
        }
    
    /**
     * 这个方法显然是有问题的,在编译器会给我们提示这样的错误信息"cannot reslove symbol E"
     * 因为在类的声明中并未声明泛型E,所以在使用E做形参和返回值类型时,编译器会无法识别。
    public E setKey(E key){
         this.key = keu
    }
    */

    /** 
    * 这才是一个真正的泛型方法。
    * 首先在public与返回值之间的<T>必不可少,这表明这是一个泛型方法,并且声明了一个泛型T
    * 这个T可以出现在这个泛型方法的任意位置.
    * 泛型的数量也可以为任意多个 
    *    如:public <T,K> K showKeyName(Generic<T> container){
    *        ...
    *        }
    */
    public <T> T showKeyName(Generic<T> container){
        System.out.println("container key :" + container.getKey());
        //当然这个例子举的不太合适,只是为了说明泛型方法的特性。
        T test = container.getKey();
        return test;
    }
    再看一个泛型方法和可变参数的例子:
    public <T> void printMsg( T... args){
        for(T t : args){
            Log.d("泛型测试","t is " + t);
        }
    }
    printMsg("111",222,"aaaa","2323.4",55.55);
```

可变参数的例子:
public void printMsg( T… args){
for(T t : args){
Log.d(“泛型测试”,"t is " + t);
}
}
printMsg(“111”,222,“aaaa”,“2323.4”,55.55);

~~~






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@来杯咖啡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值