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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王峰峰 中级黑马   /  2014-4-20 15:28  /  1032 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public class GenericsTest03 {
  2.         public static void main(String[] args) {
  3.                 Object str = "123" ;
  4.                 String x = autoConvert(str) ;
  5.         }
  6.        
  7.         //这里的返回值为什么是String
  8.         public static <T> T autoConvert(Object obj) {
  9.                 return (T)obj ;
  10.         }
  11. }
复制代码


11 个回复

倒序浏览
同学, 这里你并没有指定泛型类型的啊, 那么T就是一个占位符, 你第一个放进去是什么类型的就是什么类型的
回复 使用道具 举报
public class GenericsTest03 {
        public static void main(String[] args) {
                Object str = "123" ;
                String x = autoConvert(str) ;
        }
        
        //这里的返回值为什么是String
        public static <T> T autoConvert(Object obj) {
                return (T)obj ;
        }
}
答;    public static <T> T autoConvert(Object obj) 这是泛型方法
你传入什么类型就会返回什么类型!
回复 使用道具 举报
  1. import java.util.*;

  2. public class SunJingQi {

  3.         public static void main(String[] args) {

  4.                 ArrayList<String> al = new ArrayList<String>();

  5.                 al.add("孙旌棋");
  6.                 al.add("孙旌棋");
  7.                 al.add("孙旌棋");
  8.                 printColl(al);

  9.                 ArrayList<Integer> al_1 = new ArrayList<Integer>();

  10.                 al_1.add(863523704);
  11.                 al_1.add(863523704);
  12.                 al_1.add(863523704);
  13. //                printColl_1(al_1);
  14.                 printColl(al_1);

  15.         }

  16. //        public static void printColl(ArrayList<String> al) {
  17. //                for (Iterator<String> it = al.iterator(); it.hasNext();) {
  18. //                        System.out.println(it.next());
  19. //                }
  20. //        }
  21. //        public static void printColl_1(ArrayList<Integer> al) {
  22. //                for (Iterator<Integer> it = al.iterator(); it.hasNext();) {
  23. //                        System.out.println(it.next());
  24. //                }
  25. //        }

  26.         public static void printColl(ArrayList<?> al) {

  27.                 for (Iterator<?> it = al.iterator(); it.hasNext();) {
  28.                         System.out.println(it.next());
  29.                 }

  30.         }
  31. }
复制代码

T就相当于这里的问号, 称为通配符或者占位符, 但是第一个你存进去的是String类型的, 那么就只能存String类型的哦

评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 赞一个!

查看全部评分

回复 使用道具 举报
本帖最后由 伍叶竹 于 2014-4-20 15:48 编辑

改了下代码,这样才是泛型的应用,编译不再提示不安全操作。就一个字符串对象,返回值必然是字符串对象了。
  1. public class GenericsTest
  2. {
  3.         public static void main(String[] args) {
  4.                 String str = "123" ;
  5.                 String x = autoConvert(str) ;
  6.                                 System.out.println(x);
  7.         }
  8.         
  9.         //
  10.         public static <String> String autoConvert(String obj) {
  11.                 return (String)obj ;
  12.         }
  13. }
复制代码


回复 使用道具 举报
悠然丶 发表于 2014-4-20 15:38
public class GenericsTest03 {
        public static void main(String[] args) {
                Objec ...

可是我传进去的是Object啊,它如果返回Object,则 String x = autoConvert(str) ;这里应该报错,那它为什么没有报错啊????
回复 使用道具 举报
王峰峰 发表于 2014-4-20 15:48
可是我传进去的是Object啊,它如果返回Object,则 String x = autoConvert(str) ;这里应该报错,那它为什 ...

public class GenericsTest03 {
        public static void main(String[] args) {
                Object str = "123" ;
                String x = autoConvert(str) ;  //  请注意.这里你调用方法时传入的是str  就是字符串啊

        }
        
        //这里的返回值为什么是String
        public static <T> T autoConvert(Object obj) {
                return (T)obj ;
        }
}
回复 使用道具 举报
王峰峰 发表于 2014-4-20 15:48
可是我传进去的是Object啊,它如果返回Object,则 String x = autoConvert(str) ;这里应该报错,那它为什 ...

public class GenericsTest03 {
        public static void main(String[] args) {
                Object str = "123" ;
                String x = autoConvert(str) ;  //  请注意.这里你调用方法时传入的是str  就是字符串啊
                                                           //所以返回的就是字符串  用String类接收
        }
        
        //这里的返回值为什么是String
        public static <T> T autoConvert(Object obj) {
                return (T)obj ;
        }
}
回复 使用道具 举报
Object str = "123" ;
String是Object的子类。 这里是父类引用指向子类对象了。 说到底还是字符串对象吧?
回复 使用道具 举报
伍叶竹 发表于 2014-4-20 16:05
Object str = "123" ;
String是Object的子类。 这里是父类引用指向子类对象了。 说到底还是字符串对象吧? ...

对,但是引用是Object的,我想它返回的也应该是Ojbect,为什么是String实在想不通????
回复 使用道具 举报
王峰峰 发表于 2014-4-20 16:31
对,但是引用是Object的,我想它返回的也应该是Ojbect,为什么是String实在想不通???? ...

你查一下,父类引用指向子类对象,到底是创建了什么对象、baidu
回复 使用道具 举报
他是根据返回值来确定返回值类型的
public static <T> T autoConvert(Object obj) {
                 return (T)obj ;
         }
}
T是返回值   
如果Integer x = autoConvert(str) ;     T的类型就是Integer
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马