[转帖]Java变量类型识别的3种方式_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2503 | 回复: 0   主题: [转帖]Java变量类型识别的3种方式        下一篇 
    本主题由 koei 于 2014-5-2 16:22:01 移动
lengyuLee
注册用户
等级:少校
经验:1118
发帖:83
精华:5
注册:2013-3-7
状态:离线
发送短消息息给lengyuLee 加好友    发送短消息息给lengyuLee 发消息
发表于: IP:您无权察看 2013-3-14 14:55:40 | [全部帖] [楼主帖] 楼主

文章转载自:http://snkcxy.iteye.com/blog/1827913

变量类型识别有3种方法:

  1. 通过反射拿到变量的类型

  2. instanceof关键字判断
  3. 通过java的多态(方法重载)来DIY类型识别

具体看例子吧,尤其第三种方式 有一个自定义的TypeTools,我觉得很实用。希望大家喜欢~

Java代码  北京联动北方科技有限公司

  1. package com.cxyapi.oo; 
  2. /** 类型识别工具测试类 
  3.  * @author cxy @ www.cxyapi.com 
  4.  */ 
  5. public class TypeToolsTest 
  6.        public static void main(String[] args) 
  7.        { 
  8.              int i=0; 
  9.              TypeObject to=new TypeObject(); 
  10.              //1.反射 
  11.              System.out.println("to的类型:"+to.getClass().getSimpleName()); 
  12.              System.out.println(int.class.getSimpleName()); 
  13.              System.out.println(Integer.class.getSimpleName()); 
  14.              //但是对于一个不确定类型的基本数据类型变量我们没法用反射来获取其类型。 
  15.              System.out.println("----------------------"); 
  16.             
  17.              //2.instanceof 
  18.        if(to instanceof TypeObject){ System.out.println("to是TypeObject类型的");} 
  19.              //但是这种办法貌似也没法确定基本数据类型 
  20.              System.out.println("----------------------"); 
  21.             
  22.              //以上两种方式对于对象,引用类型的都很好用,但是对基本数据类型就不那么好用了。 
  23.              //3.通过多态(方法的重载) 
  24.              System.out.println("i是:"+TypeTools.getType(i)); 
  25.              System.out.println("to是:"+TypeTools.getType(to)); 
  26.              System.out.println("\"cxyapi\"是:"+TypeTools.getType("www.cxyapi.com")); 
  27.              //大家可以看出来 最后一种方式使用多态的方式达到了检测类型(基本类型和引用类型)的目的 
  28.              //它除了弥补其他两种方式不能检测基本数据类型的不足在外,还能自己DIY类型信息 
  29.        } 
  30. //定义一个类,为了演示引用类型的类型检测 
  31. class TypeObject{} 


 自定义的类型识别工具:
Java代码  北京联动北方科技有限公司

  1. package com.cxyapi.oo; 
  2. import java.util.HashMap; 
  3. import java.util.Map; 
  4. /** 类型识别工具 
  5.  * @author cxy @ www.cxyapi.com 
  6.  */ 
  7. public class TypeTools 
  8.        //获得类型 
  9.        public static Map<String,String> getType(Object o) 
  10.        { 
  11.              Map<String,String> typeInfo=new HashMap<String,String>(); 
  12.              typeInfo.put("类型", o.getClass().getSimpleName()); 
  13.              typeInfo.put("描述", "引用类型"); 
  14.              return typeInfo; 
  15.        } 
  16.       
  17.        public static Map<String,String> getType(int i) 
  18.        { 
  19.              Map<String,String> typeInfo=new HashMap<String,String>(); 
  20.              typeInfo.put("类型", "int"); 
  21.              typeInfo.put("描述", "整形"); 
  22.              return typeInfo; 
  23.        } 
  24.       
  25.        public static Map<String,String> getType(long l) 
  26.        { 
  27.              Map<String,String> typeInfo=new HashMap<String,String>(); 
  28.              typeInfo.put("类型", "long"); 
  29.              typeInfo.put("描述", "长整型"); 
  30.              return typeInfo; 
  31.        } 
  32.       
  33.        public static Map<String,String> getType(boolean b) 
  34.        { 
  35.              Map<String,String> typeInfo=new HashMap<String,String>(); 
  36.              typeInfo.put("类型", "boolean"); 
  37.              typeInfo.put("描述", "布尔类型"); 
  38.              return typeInfo; 
  39.        } 
  40.       
  41.        public static Map<String,String> getType(char b) 
  42.        { 
  43.              Map<String,String> typeInfo=new HashMap<String,String>(); 
  44.              typeInfo.put("类型", "char"); 
  45.              typeInfo.put("描述", "字符"); 
  46.              return typeInfo; 
  47.        } 
  48.       
  49.        public static Map<String,String> getType(float f) 
  50.        { 
  51.              Map<String,String> typeInfo=new HashMap<String,String>(); 
  52.              typeInfo.put("类型", "float"); 
  53.              typeInfo.put("描述", "单精度浮点型"); 
  54.              return typeInfo; 
  55.        } 
  56.       
  57.        public static Map<String,String> getType(double d) 
  58.        { 
  59.              Map<String,String> typeInfo=new HashMap<String,String>(); 
  60.              typeInfo.put("类型", "double"); 
  61.              typeInfo.put("描述", "双精度浮点型"); 
  62.              return typeInfo; 
  63.        } 
  64.       
  65.        public static Map<String,String> getType(String s) 
  66.        { 
  67.              Map<String,String> typeInfo=new HashMap<String,String>(); 
  68.              typeInfo.put("类型", "String"); 
  69.              typeInfo.put("描述", "字符串类型"); 
  70.              return typeInfo; 
  71.        } 
  72.       


该贴由koei转至本版2014-5-2 16:22:01



赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论