zoukankan      html  css  js  c++  java
  • JAVA中如何取得一个变量的类型

    class Test 
    {
    public static void main(String[] args) 
    {
    int i=1;
    System.out.println(getType(i));
    }
    public static String getType(Object o){
    return o.getClass().toString();
    }
    }

    public class Test15 {
    public static void main(String[] args) 
    {
    int i=1;
    Integer i1 = 2;
    double d = 1.8;
    long l = 76;
    boolean b = false;
    
    System.out.println(getType(i));
    System.out.println(getType(i1));
    System.out.println(getType(d));
    System.out.println(getType(l));
    System.out.println(getType(b));
    }
    public static String getType(Object o){
    return o.getClass().toString();
    }
    public static String getType(int o){
    return "int";
    }
    public static String getType(byte o){
    return "byte";
    }
    public static String getType(char o){
    return "char";
    }
    public static String getType(double o){
    return "double";
    }
    public static String getType(float o){
    return "float";
    }
    public static String getType(long o){
    return "long";
    }
    public static String getType(boolean o){
    return "boolean";
    }
    public static String getType(short o){
    return "short";
    }
    }

    obj.getClass().getName()   ===〉    java.lang.Integer
    obj.getClass().toString()  ===〉    class java.lang.Integer

    public static void getType(Object object) {
    int length = object.getClass().getName().lastIndexOf(".");
    String type = object.getClass().getName().substring(length + 1);
    System.out.println(type);
    }
    基本类型不能得到

  • 相关阅读:
    python D32 管道、线程池
    python D31 守护进程、进程锁、队列
    python D30 进程
    python 30 进程之间的相互独立、进程之间的时间差
    python D30 操作系统历史
    python D29 socketserver以及FTB
    python D28 粘包
    net4.0 task 超时任务代码 用Thread.sleep方式实现
    sql取随机结果集
    【ecshop---新增包邮卡功能】
  • 原文地址:https://www.cnblogs.com/timssd/p/10298779.html
Copyright © 2011-2022 走看看