泛型&反射



1. 泛型


1.1基本知识:

基本用法、泛型擦除、泛型类/泛型方法/泛型接口、泛型关键字、反射泛型(案例)

 作用:   泛型是JDK1.5以后才有的, 可以在编译时期进行类型检查,且可以避免频繁类型转化!

1.2 泛型擦除:   泛型只在编译时期有效,编译后的字节码文件中不存在有泛型信息!






/*

     * 泛型擦除实例

     

    public void save(List<Person> p){

    }

    public void save(List<Dept> d){    // 报错: 与上面方法编译后一样

    }

    */










1.3.泛型写法:

@Test


    public void testGeneric3() throws Exception {


        // 声明泛型集合,集合两端类型必须一致


        List<Object> list = new ArrayList<Object>();


        List<String> list1 = new ArrayList<String>();


        List list2 = new ArrayList<String>();


        List<Integer> list3 = new ArrayList();


       


        // 错误


        //List<Object> list4 = newArrayList<String>();


        // 错误: 泛型类型必须是引用类型,不能为基本类型


        List<int> list5 = new ArrayList<int>();

    }


1.4泛型方法

public class GenericDemo {

 

    // 定义泛型方法

    public <K,T>T save(T t,K k) {

        return null;

    }

   

    // 测试方法

    @Test

    public void testMethod() throws Exception {

        // 使用泛型方法:  在使用泛型方法的时候,确定泛型类型

        save(1.0f, 1);

    }

}

1.5泛型类:

public class GenericDemo<T> {

 

    // 定义泛型方法

    public <K> T save(T t,K k) {

        return null;

    }

   

    public void update(T t) {

 

    }

   

    // 测试方法

    @Test

    public void testMethod() throws Exception {

       

        // 泛型类:  在创建爱泛型类对象的时候,确定类型

        GenericDemo<String> demo = new GenericDemo<String>();

        demo.save("test", 1);

    }

}

1.6泛型接口



/**

 * 泛型接口

 * @author Jie.Yuan

 *

 * @param <T>

 */

public interface IBaseDao<T> {

    void save(T t );

    void update(T t );

}

 

 

泛型接口类型确定: 实现泛型接口的类也是抽象,那么类型在具体的实现中确定或创建泛型类的时候确定

public class BaseDao<T> implements IBaseDao<T> {

 

泛型接口类型确定: 在业务实现类中直接确定接口的类型

public class PersonDao implements IBaseDao<Person>{

 

 


1.7 泛型关键字:


泛型中:


?                               指定只是接收值


extends      元素的类型必须继承自指定的类   上限


super        元素的类型必须是指定的类的父类   下限


快捷键:

Ctrl + shift + R   查看当前项目中类


 Ctrl + shift + T   查看源码jar包中的类


1.8 泛型的反射:


案例,设置通用方法时,会用到反射泛型!



反射泛型涉及API


Student   类型的表示


Id   name


ParameterizedType   参数化类型的表示


ArrayList<String>();


 


Type   接口,任何类型默认的接口!


       包括:引用类型、原始类型、参数化类型


 


List<String> list   =  new  ArrayList<String>();


泛型集合:    list


集合元素定义:new  ArrayList<String>();  中的String


参数化类型:  ParameterizedType


即:“ArrayList<String> 为参数化类型



public class AdminDao extends BaseDao<Admin> {}

public class AccountDao extends BaseDao<Account> {}

 

 

/**

 * 所有dao的公用的方法,都在这里实现

 * @author Jie.Yuan

 *

 */

public class BaseDao<T>{

   

    // 保存当前运行类的参数化类型中的实际的类型

    private Class clazz;

    // 表名

    private String tableName;

   

   

   

    // 构造函数: 1. 获取当前运行类的参数化类型; 2. 获取参数化类型中实际类型的定义(class)

    public BaseDao(){

        //  this  表示当前运行类  (AccountDao/AdminDao)

        //  this.getClass()  当前运行类的字节码(AccountDao.class/AdminDao.class)

        //  this.getClass().getGenericSuperclass();  当前运行类的父类,即为BaseDao<Account>

        //                                           其实就是参数化类型 ParameterizedType  

        Type type = this.getClass().getGenericSuperclass();

        // 强制转换为参数化类型  BaseDao<Account>

        ParameterizedType pt = (ParameterizedType) type;

        // 获取参数化类型中,实际类型的定义  new Type[]{Account.class}

        Type types[] =  pt.getActualTypeArguments();

        // 获取数据的第一个元素:Accout.class

        clazz = (Class) types[0];

        // 表名  (与类名一样,只要获取类名就可以)

        tableName = clazz.getSimpleName();

    }

   

 

    /**

     * 主键查询

     * @param id    主键值

     * @return      返回封装后的对象

     */

    public T findById(int id){

        /*

         * 1. 知道封装的对象的类型

         * 2. 表名【表名与对象名称一样, 且主键都为id

         *

         * 即,

         *   ---》得到当前运行类继承的父类  BaseDao<Account>

         *   ---- 得到Account.class

         */

       

        String sql = "select * from " + tableName + " where id=? ";

        try {

            return JdbcUtils.getQuerrRunner().query(sql, new BeanHandler<T>(clazz), id);

        } catch (SQLException e) {

            throw new RuntimeException(e);

        }

    }

   

   

    /**

     * 查询全部

     * @return

     */

    public List<T> getAll(){

        String sql = "select * from " + tableName ;

        try {

            return JdbcUtils.getQuerrRunner().query(sql, new BeanListHandler<T>(clazz));

        } catch (SQLException e) {

            throw new RuntimeException(e);

        }

    }

}

 


反射,可以在运行时期动态创建对象;获取对象的属性、方法;

 










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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值