zoukankan      html  css  js  c++  java
  • getSuperclass与getGenericSuperclass区别

    声明三个类
    class Person<T, V> {
    }
    class Teacher {
    }
    class Student extends Person<Student, Teacher> {
    }

    分别打印数据观察区别
    Class<? super Student> superclass = Student.class.getSuperclass();
    Type genericSuperclass = Student.class.getGenericSuperclass();
    System.out.println(superclass);
    System.out.println(genericSuperclass);

    输出结果:
    class com.test.Person
    com.test.Person<com.test.Student, com.test.Teacher>

    总结
    相同点:都返回当前对象所表示的类的超类。
    不同点:getGenericSuperclass会包含该超类的泛型。

    补充
    getGenericSuperclass既然可以获取超类的泛型肯定能取出指定的泛型。
    实现代码如下
    Type[] params = ((ParameterizedType) genericSuperclass).getActualTypeArguments();
    System.out.println((Class) params[1]);

    输出
    class com.test.Teacher
    原文:https://blog.csdn.net/jingyangV587/article/details/81037054

    具体使用例子,如在基础dao中操作hibernate查询时,需要知道当前操作的类的类型(可能是student,teacher,manager等),而具体实现类通常是实现其子类(如子类userDaoimpl),此时我们需要通过子类(即子类this)确定父类的泛型类型basedaoiml<A,B,C>。结合上面知识点,可理解下面红色部分代码实现的原因。

    package com.itheima.bos.dao.base.impl;
    
    import java.io.Serializable;
    import java.lang.reflect.ParameterizedType;
    import java.lang.reflect.Type;
    import java.util.List;
    import javax.annotation.Resource;
    
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    import com.itheima.bos.dao.base.IBaseDao;
    
    /**
     * 持久层通用实现
     */
    public class BaseDaoImpl<T> extends HibernateDaoSupport implements IBaseDao<T> {
        // 实体类型
        private Class<T> entityClass;
    
        
        // 使用注解方式进行依赖注入
        //@Resource
        @Autowired
        // @Qualifier(value="abc")
        public void setMySessionFactory(SessionFactory sessionFactory) {
            super.setSessionFactory(sessionFactory);
        }
    
        /**
         * 在构造方法中动态获得操作的实体类型
         */
        public BaseDaoImpl() {
            // 获得父类(BaseDaoImpl<T>)类型
            ParameterizedType genericSuperclass = (ParameterizedType) this
                    .getClass().getGenericSuperclass();
            // 获得父类上的泛型数组
            Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
            entityClass = (Class<T>) actualTypeArguments[0];
        }
    
        public void save(T entity) {
            this.getHibernateTemplate().save(entity);
        }
    
        public void delete(T entity) {
            this.getHibernateTemplate().delete(entity);
        }
    
        public void update(T entity) {
            this.getHibernateTemplate().update(entity);
        }
    
        public T findById(Serializable id) {
            return this.getHibernateTemplate().get(entityClass, id);
        }
    
        public List<T> findAll() {// FROM User
            String hql = "FROM  " + entityClass.getSimpleName();
            return this.getHibernateTemplate().find(hql);
        }
    
    }
  • 相关阅读:
    Tizen Sample Web Applications
    Linux下RPM软件包的安装及卸载
    libevent
    GTest 运行参数
    【BBC micro:bit基础教程】02-micro:bit与人体运动检测传感器
    【BBC micro:bit基础教程】01-如何用按键控制一个LED
    CMD下查询Mysql中文乱码的解决方法
    php foreach 使用&(与运算符)引用赋值要注意的问题
    sql必知必会(第四版) 学习笔记一
    test
  • 原文地址:https://www.cnblogs.com/theWinter/p/10118070.html
Copyright © 2011-2022 走看看