1 class Test { 2 public static void main(String[] args) { 3 int i = 1; //int类型变量 4 System.out.println(getType(i)); //打印变量类型为int 5 6 User user = new User(); 7 System.out.println(user.getClass()); 8 System.out.println(user.getClass().toString()); 9 } 10 11 public static String getType(Object o) { //获取变量类型方法 12 return o.getClass().toString(); //使用int类型的getClass()方法 13 } 14 }
User类:
1 public class User { 2 private String name; 3 private String password; 4 5 //getter setter 6 public String getName() { 7 return name; 8 } 9 10 public void setName(String name) { 11 this.name = name; 12 } 13 14 public String getPassword() { 15 return password; 16 } 17 18 public void setPassword(String password) { 19 this.password = password; 20 } 21 }
运行结果:
参考:https://www.cnblogs.com/smuxiaolei/p/7692392.html