Java上路13-泛型


       泛型是JDK1.5为类型安全做的更新,将运行时期出现的ClassCaseException转移到了编译时期。通过尖括号定义要引用的数据类型。

import java.util.*;
 
class GenericDemo
{
       public static void main(String[] args)
       {
              //使用尖括号声明String类型的集合
              TreeSet<String> al=new TreeSet<String>(new MyComparator());
 
              al.add("abcde");
              al.add("bbeed");
              al.add("ddfee");
 
              //迭代器也得声明
              Iterator<String> it=al.iterator();
              while(it.hasNext())
              {
                     String s=it.next();       //省略强转
                     System.out.println(s);
              }
       }
}
 
//比较器
class MyComparator implements Comparator<String>
{
       public int compare(String o1, String o2)
       {
              int num=new Integer(o2.length()).compareTo(new Integer(o1.length()));
 
              if(num==0)
                     return o2.compareTo(o1);
 
              return num;
       }
}    


一. 自定义泛型类:

       当类中要操作的引用数据类型不确定时,可以自定义泛型类扩展。

import java.util.*;
 
class Worker
{
       Worker()
       {
              System.out.println("泛型...");
       }
}
 
class CustomGeneric<MyClass> //自定义的泛型类
{
       private MyClass mc;
       public void setObject(MyClass mc)
       {
              this.mc=mc;
       }
       public MyClass getObject()
       {
              return mc;
       }
}
 
class GenericDemo
{
       public static void main(String[] args)
       {
              CustomGeneric<Worker> cg=new CustomGeneric<Worker>();
              cg.setObject(new Worker());
              System.out.println(cg.getObject());
       }
}    


二. 泛型方法:

       为了让不同方法操作不同类型,将泛型定义在方法上。

import java.util.*;
 
class CustomGeneric
{
       public<Custom> void showCustom(Custom c)     //自定义的泛型方法
       {
              System.out.println("show"+c);
       }
}
 
class GenericDemo
{
       public static void main(String[] args)
       {
              CustomGeneric cg=new CustomGeneric();
              cg.showCustom("HelloGeneric");
              cg.showCustom(123);
       }
}  

   

三. 泛型类和泛型方法的混合使用:

import java.util.*;
 
class CustomGeneric<Custom> //泛型类
{
       public void showCustom(Custom c)  //追随类的泛型方法
       {
              System.out.println("show"+c);
       }
 
       public <Cust> void printCustom(Custc)  //自定义的泛型方法
       {
              System.out.println("print"+c);
       }
 
       //静态的泛型方法
       public static <CustomStatoc> void staticCustom (CustomStatoc cs)
       {
              System.out.println("static"+cs);
       }
}
 
classGenericDemo
{
       public static void main(String[] args)
       {
              CustomGeneric<String> cg=new CustomGeneric<String>();
              cg.showCustom("HelloGeneric");
 
              cg.printCustom(123);
              cg.printCustom("abc");
 
              CustomGeneric.staticCustom(46);
              CustomGeneric.staticCustom("tom");
       }
}  

   

四. 泛型接口:

import java.util.*;
 
interface Inter<T>
{
       void show(T t);
}
 
class InterImp<Ttt> implements Inter<Ttt>
{
       public void show(Ttt t)
       {
              System.out.println(""+t);
       }
}
 
class GenericDemo
{
       public static void main(String[] args)
       {
              InterImp<Integer> i=new InterImp<Integer>();
              i.show(469);
       }
}   


五. 泛型限定:

       ?通配符,泛型方法中替代不确定类型的元素,

import java.util.*;
 
class GenericDemo
{
       public static void main(String[] args)
       {
              ArrayList<String> als=new ArrayList<String>();
              als.add("dfk");
              als.add("jhutyr");
              als.add("sdfh");
              als.add("kgrere");
 
              ArrayList<Integer> ali=new ArrayList<Integer>();
              ali.add(14);
              ali.add(24);
              ali.add(50);
              ali.add(23);
 
              printColl(als);
              printColl(ali);
       }
 
       public static void printColl(ArrayList<?> al)
       {
              Iterator<?> it=al.iterator();
              while (it.hasNext())
              {
                     System.out.println(it.next());
              }
       }
}   


       ? extends E,可以接受E类型或E的子类,设置了上限;

       ? super E,可以接受E或E的父类,设置了下限;

import java.util.*;
 
class GenericDemo
{
       public static void main(String[] args)
       {
              ArrayList<Person> alp=new ArrayList<Person>();
              alp.add(new Person("张三"));
              alp.add(new Person("李四"));
              alp.add(new Person("王五"));
              printColl(alp);
 
              System.out.println();
 
              ArrayList<Student> als=new ArrayList<Student>();
              als.add(new Student("学生1"));
              als.add(new Student("学生2"));
              als.add(new Student("学生3"));
              printColl(als);
 
              System.out.println();
 
              TreeSet<Student> tss=new TreeSet<Student>(new MyComparator());
              tss.add(new Student("学生4"));
              tss.add(new Student("学生2"));
              tss.add(new Student("学生3"));
              tss.add(new Student("学生5"));
              for(Iterator<Student>itt=tss.iterator();itt.hasNext();)
              {
                     Student s=itt.next();
                     System.out.println(s.getName());
              }
       }
 
       //只能引用Person或子类实例
       public static void printColl(ArrayList<? extends Person> al)
       {
              Iterator<?> it=al.iterator();
              while (it.hasNext())
              {
                     Person p=(Person)it.next();
                     System.out.println(p.getName());
              }
       }
}
 
//Person的所有子类都可引用
class MyComparator implements Comparator<Person>
{
       public int compare(Person p1, Person p2)
       {
              return p1.getName().compareTo(p2.getName());
       }
}
 
class Person
{
       private String name;
 
       Person(String name)
       {
              this.name=name;
       }
 
       public String getName()
       {
              return name;
       }
}
 
class Student extends Person
{
       Student(String name)
       {
              super(name);
       }
}   

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值