zoukankan      html  css  js  c++  java
  • Android(java)学习笔记29:泛型类的概述和使用

    1. 泛型类的概述和使用

    泛型类用法一:

    下面我们首先定义泛型类:

     1 package cn.itcast_04;
     2 
     3 /*
     4  * 泛型类:把泛型定义在类上
     5  */
     6 public class ObjectTool<T> {  //这里的<T>就像一个参数一样,现在还不确定这个泛型的类型是什么,之后创建对象或者调用方法的时候才会知道
     7     private T obj;
     8 
     9     public T getObj() {
    10         return obj;
    11     }
    12 
    13     public void setObj(T obj) {
    14         this.obj = obj;
    15     }
    16 }

    接下来我编写测试类应用上面的泛型类 ObjectTool<T>:

     1 package cn.itcast_04;
     2 
     3 /*
     4  * 泛型类的测试
     5  */
     6 public class ObjectToolDemo {
     7     public static void main(String[] args) {
     8         // ObjectTool ot = new ObjectTool();
     9         //
    10         // ot.setObj(new String("风清扬"));
    11         // String s = (String) ot.getObj();
    12         // System.out.println("姓名是:" + s);
    13         //
    14         // ot.setObj(new Integer(30));
    15         // Integer i = (Integer) ot.getObj();
    16         // System.out.println("年龄是:" + i);
    17 
    18         // ot.setObj(new String("林青霞"));
    19         // // ClassCastException
    20         // Integer ii = (Integer) ot.getObj();
    21         // System.out.println("姓名是:" + ii);
    22 
    23         System.out.println("-------------");
    24 
    25         ObjectTool<String> ot = new ObjectTool<String>();
    26         // ot.setObj(new Integer(27)); //这个时候编译期间就过不去
    27         ot.setObj(new String("林青霞"));
    28         String s = ot.getObj();
    29         System.out.println("姓名是:" + s);
    30 
    31         ObjectTool<Integer> ot2 = new ObjectTool<Integer>();
    32         // ot2.setObj(new String("风清扬"));//这个时候编译期间就过不去
    33         ot2.setObj(new Integer(27));
    34         Integer i = ot2.getObj();
    35         System.out.println("年龄是:" + i);
    36     }
    37 }

    泛型类用法二:泛型之方法

     1 package cn.itcast_05;
     2 
     3 //public class ObjectTool<T> {
     4 //    // public void show(String s) {
     5 //    // System.out.println(s);
     6 //    // }
     7 //    //
     8 //    // public void show(Integer i) {
     9 //    // System.out.println(i);
    10 //    // }
    11 //    //
    12 //    // public void show(Boolean b) {
    13 //    // System.out.println(b);
    14 //    // }
    15 //
    16 //    public void show(T t) {
    17 //        System.out.println(t);
    18 //    }
    19 // }
    20 
    21 /*
    22  * 泛型方法:把泛型定义在方法上
    23  */
    24 public class ObjectTool {
    25     public <T> void show(T t) {
    26         System.out.println(t);
    27     }
    28 }

    然后编写泛型测试类:

     1 package cn.itcast_05;
     2 
     3 public class ObjectToolDemo {
     4     public static void main(String[] args) {
     5         // ObjectTool ot = new ObjectTool();
     6         // ot.show("hello");
     7         // ot.show(100);
     8         // ot.show(true);
     9 
    10         // ObjectTool<String> ot = new ObjectTool<String>();
    11         // ot.show("hello");
    12         //
    13         // ObjectTool<Integer> ot2 = new ObjectTool<Integer>();
    14         // ot2.show(100);
    15         //
    16         // ObjectTool<Boolean> ot3 = new ObjectTool<Boolean>();
    17         // ot3.show(true);
    18 
    19         // 如果还听得懂,那就说明泛型类是没有问题的
    20         // 但是呢,谁说了我的方法一定要和类的类型的一致呢?
    21         // 我要是类上没有泛型的话,方法还能不能接收任意类型的参数了呢?
    22 
    23         // 定义泛型方法后
    24         ObjectTool ot = new ObjectTool();
    25         ot.show("hello");
    26         ot.show(100);
    27         ot.show(true);
    28     }
    29 }
  • 相关阅读:
    django报错信息解决办法
    postgresql数据库的基本使用
    Ubuntu 18.04中安装PostgreSQL(PSQL)
    Ubuntu 18.04 安装 PyCharm
    在Ubuntu中安装Docker和docker的使用
    ubuntu怎么切换到root用户,切换到root账号方法
    centos7彻底卸载mysql和通过yum安装mysql
    django运行时报错处理方法
    Django 查询时间段 时间搜索 过滤
    View Controller Programming Guide for iOS---(四)---Creating Custom Content View Controllers
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4529959.html
Copyright © 2011-2022 走看看