Java变量类型识别的3种方式

来源:互联网 发布:python文档 编辑:程序博客网 时间:2024/06/06 12:41

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

  1. 通过反射拿到变量的类型
  2. instanceof关键字判断
  3. 通过java的多态(方法重载)来DIY类型识别

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

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

 

    自定义的类型识别工具:

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