Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 323|回复: 0

[泛型学习]简单的范型实例程序

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-30 23:56:43 | 显示全部楼层 |阅读模式
    1. import java.util.ArrayList;
    2. import java.util.Collection;
    3. import java.util.List;

    4. /**
    5. * 范型编程
    6. */

    7. // 父类
    8. class Parent{
    9.         public String name;
    10.         public Parent(String name){
    11.                 this.name = name;
    12.         }
    13.         public String toString(){
    14.                 return "name = " + this.name;
    15.         }
    16. }
    复制代码

      
      
    1. // 子类
    2. class Child extends Parent{
    3.         public int age;
    4.         public Child(String name, int age){
    5.                 super(name);
    6.                 this.age = age;
    7.         }
    8.         public String toString(){
    9.                 return super.toString() + ";  age = " + age;
    10.         }
    11. }

    12. public class GeneralProgram {
    13.         /**
    14.            * 在命令行输出一个集
    15.          * 使用问号?通配符,?代表任何类型,所以它的参数可以是任何类型的Collection。
    16.          */
    17.         public static void printCollection(Collection< ?> collect){
    18.                 if (collect == null){
    19.                         return;
    20.                 }
    21.                 for(Object obj : collect){
    22.                         System.out.print(obj + "    ");
    23.                 }
    24.                 System.out.println();
    25.         }
    26.            /**
    27.          * 使用有限制的通配符“? extends”,可以接受任何Parent及其子类的Collection
    28.          * @param collect
    29.          */
    30.         public static void printNames(Collection< ? extends Parent> collect){
    31.                 if (collect == null){
    32.                         return;
    33.                 }
    34.                 for(Parent parent : collect){
    35.                         System.out.print(parent.name + "    ");
    36.                 }
    37.                 System.out.println();
    38.         }
    39.    
    40.      /**
    41.          * 范型方法,将一个任意类型的数组,添加到列表中。
    42.          * @param < T> 代表一个任意的类
    43.          * @param datas        数组对象
    44.          * @return
    45.          */
    46.        
    47.     public static < T> List< T> arrayToList(T[] datas){
    48.                 if (datas == null){
    49.                         return null;
    50.                 }
    51.                 List< T> list_T = new ArrayList< T>();
    52.                 for (T x : datas){
    53.                         list_T.add(x);
    54.                 }
    55.                 return list_T;
    56.         }
    57.         /**
    58.          * 范型方法,在一组Parent对象中查找名字为name的Parent对象
    59.          * @param < T>        可以是Parent对象或者子类对象
    60.          * @param parents        Parent对象组
    61.          * @param name        目标name
    62.          * @return        匹配的Parent对象
    63.          */
    64.        
    65.      public static < T extends Parent> T findParent(T[] parents, String name) {
    66.                 if (parents == null) {
    67.                         return null;
    68.                 }
    69.                 T parent = null;
    70.                 // 依次遍历Parent对象组
    71.                 for (T x : parents) {
    72.                         // 如果Parent对象的name与参数name匹配,则退出遍历
    73.                         if (x.name.equals(name)) {
    74.                                 parent = x;
    75.                                 break;
    76.                         }
    77.                 }
    78.                 // 返回
    79.                 return parent;
    80.         }
    81.        
    82.         public static void main(String[] args) {
    83.                 /** * 指定具体的类型 ** */
    84.                 // 声明一个用于装字符串的列表,该列表只能装字符串类型的对象
    85.                 List< String> list_S = new ArrayList< String>();
    86.                 list_S.add("first");
    87.                 list_S.add("second");
    88.                 list_S.add("third");
    89.                
    90.         // 不能装整数对象
    91.                 Integer iObj = 10;
    92.                 // list_S.add(iObj);// error!!!
    93.                 // 在从列表中取值时,不用作强制类型转换。
    94.                 String firstS = list_S.get(0);
    95.                 String thirdS = list_S.get(2);
    96.                 System.out.println("firstS: " + firstS + "; thirdS: " + thirdS);
    97.                
    98.         /** **范型和继承** */
    99.                 // String 继承 Object
    100.                 String s = "sss";
    101.                 Object o = s;
    102.                 // 但List< String>不继承List< Object>
    103.                 // List< Object> list_O = list_S;// error!!!
    104.                
    105.         /** * 通配符 *** */
    106.                 // 调用具有“?”通配符的方法
    107.                 List< Integer> list_I = new ArrayList< Integer>();
    108.                 list_I.add(5555);
    109.                 list_I.add(6666);
    110.                 list_I.add(7777);
    111.                 // 该方法既可以打印整型列表,也可以打印字符串列表
    112.                 printCollection(list_I);
    113.                 printCollection(list_S);
    114.                
    115.         // 调用具有“? extends” 通配符的方法
    116.                 // 只接受父类以及子类类型的列表
    117.                 List< Parent> list_Parent = new ArrayList< Parent>();
    118.                 list_Parent.add(new Parent("parentOne"));
    119.                 list_Parent.add(new Parent("parentTow"));
    120.                 list_Parent.add(new Parent("parentThree"));
    121.                 List< Child> list_Child = new ArrayList< Child>();
    122.                 list_Child.add(new Child("ChildOne", 20));
    123.                 list_Child.add(new Child("ChildTow", 22));
    124.                 list_Child.add(new Child("ChildThree", 21));
    125.                 printNames(list_Parent);
    126.                 printNames(list_Child);
    127.                
    128.         // 不能接受其他类型的参数
    129.                 // printNames(list_S);// error!!!
    130.                 /** * 范型方法 *** */
    131.                
    132.         // arrayToList方法将任意类型的对象数组变成相应的列表
    133.                 Integer[] iObjs = { 55, 66, 77, 88, 99 };
    134.                 // 转换整型数组
    135.                 List< Integer> result_I = arrayToList(iObjs);
    136.                 printCollection(result_I);
    137.                
    138.         String[] ss = { "temp", "temptemp", "hehe", "he", "hehehe" };
    139.                 // 转换字符串数组
    140.                 List< String> result_S = arrayToList(ss);
    141.                 printCollection(result_S);
    142.                 // findParent方法在一组Parent对象中根据name查找Parent
    143.                 Parent[] parents = { new Parent("abc"), new Parent("bcd"),new Parent("def") };
    144.                
    145.         Parent parent = findParent(parents, "bcd");
    146.                 System.out.println("找到的bcd:" + parent);
    147.                 Child[] children = { new Child("abc", 22), new Child("bcd", 23),ew Child("def", 22) };
    148.                 Child child = findParent(children, "bcd");
    149.                 System.out.println("找到的bcd:" + child);
    150.                 // 但是不能在字符串数组中进行查找
    151.                 // String sss = findParent(ss, "temp");// error!!!
    152.         }
    153. }
    154. 运行结果:
    155. C:java>java   GeneralProgram
    156. firstS: first; thirdS: third
    157. 5555    6666    7777
    158. first    second    third
    159. parentOne    parentTow    parentThree
    160. ChildOne    ChildTow    ChildThree
    161. 55    66    77    88    99
    162. temp    temptemp    hehe    he    hehehe
    163. 找到的bcd:name = bcd
    164. 找到的bcd:name = bcd;  age = 23

    165.                      
    复制代码

       
         
         
          
          

            
          

            
          
         
       

    1.                         
    复制代码

      


    源码下载:http://file.javaxxz.com/2014/10/30/235643328.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-19 01:31 , Processed in 0.340814 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表