A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王敏NO.09 中级黑马   /  2012-4-23 18:05  /  2870 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

泛型中可以同时有多个参数类型,没见过这种情况的,谁能给个例子,谢谢

4 个回复

倒序浏览
Map<String, Integer> map = new HashMap<String, Integer>()
回复 使用道具 举报
//泛型集合的综合应用案例
                HashMap<String,Integer> maps = new HashMap<String,Integer>();
                maps.put("Wade", 28);
                maps.put("Paul", 25);
                maps.put("Bryant", 34);
                Set<Map.Entry<String,Integer>> entrySet = maps.entrySet();
                for(Map.Entry<String, Integer> entry : entrySet)
                {
                        System.out.println(entry.getKey()+":"+entry.getValue());
                }
回复 使用道具 举报
带两个类型参数的泛型。
public class twoGen<T,V>{
T ob1;
V ob2;
//构造方法也可以使用这两个类型参数
twoGen(T o1, V o2){
ob1 = o1;
ob2 = o2;
}
//显示T和V的类型
void showTypes(){
System.out.println("Type of T is "+ob1.getClass().getName());
System.out.println("Type of V is "+ob2.getClass().getName());
}
T getOb1(){
return ob1;
}
V getOb2(){return ob2;
}
}
回复 使用道具 举报
  1. public class Person<T, P> {
  2.   private T t;
  3.   private P p;
  4. public Person(T t,P p){
  5.   this.t = t;
  6.   this.p = p;
  7. }
  8. public T getT() {
  9. return t;
  10. }
  11. public void set(T t) {
  12.    this.t = t;
  13. }
  14. public P getP() {
  15.    return p;
  16. }
  17. public void setP(P p) {
  18.   this.p = p;
  19. }
  20. public void showType(){
  21.       System.out.println("当前实例化对象的两个参数的类型为:T是"+t.getClass().getName()+"类型; P是"+p.getClass().getName()+"类型;");
  22. }
  23.   public static void main(String[] args) {

  24.      Person<String,Long> per = new Person<String,Long>("aa",new Long(10));
  25.      System.out.println("当前实例化对象的属性t的值为:"+per.getT());
  26.      System.out.println("当前实例化对象的属性p的值为:"+per.getP());
  27.      per.showType();

  28. }
  29. }
复制代码
泛型的实现方式:泛型类、泛型接口和泛型方法。
泛型类的一般定义形式:访问限制修饰符 Class 类名<类型参数列表>{}
泛型类对象的申明形式:类名<类型参数列表> 变量名 = new 类名<类型参数列表>(参数值列表);


回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马