zoukankan      html  css  js  c++  java
  • 泛型的应用

    泛型是不能被实例化的。所以会存在类型转换异常。

    private Class clazz;//clazz中存储了当前操作的类型
        public BaseServiceImpl(){
            System.out.println("this代表当前调用构造方法的对象"+this);
            System.out.println("获取当前this对象的父类信息"+this.getClass().getSuperclass());
            System.out.println("获取当前this对象的父类信息(包括泛型)"+this.getClass().getGenericSuperclass());
            ParameterizedType type=(ParameterizedType)this.getClass().getGenericSuperclass();
            clazz=(Class)type.getActualTypeArguments()[0];
        
        }
        @Override
        public void delete(int id) {
            String hql="DELETE "+clazz.getSimpleName()+" where id=id";
            getSession().createQuery(hql).setInteger("id", id).executeUpdate();
        }

    ModelDriven拦截器:把返回的对象压入栈顶中

    必须要实现getModel()方法。用到泛型。

    protected T model;
        @Override
        public T getModel() {
            ParameterizedType type=(ParameterizedType)this.getClass().getGenericSuperclass();
            Class clazz=(Class)type.getActualTypeArguments()[0];
            try {
                model=(T)clazz.newInstance();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return model;
        }
        
  • 相关阅读:
    广度遍历有向图
    坚持的力量 第二十一篇
    坚持的力量 第二十二篇
    搜索引擎首页
    安装ubuntu
    最小生成树之Kruskal算法
    最小生成树之PRIM算法
    文件同步软件
    [恢]hdu 2151
    [恢]hdu 1396
  • 原文地址:https://www.cnblogs.com/bulrush/p/5871712.html
Copyright © 2011-2022 走看看