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(); } }
通过JAVA反射机制,建议楼主看看java.lang.reflect这个包.
受点累给1楼的加点东西...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"; } }
zephyr_cc() 写那么东西没用obj.getClass().getName() ===〉 java.lang.Integer obj.getClass().toString() ===〉 class java.lang.Integer
zephyr_cc()判断基本类型的是需要加上这些方法
public void getType(Object object){ int length= object.getClass().getClassName().lastIndexOf("."); String type = object.getClass().getClassName().subSstring(length+1); System.out.prinfln(type); }
不好意思修改以上代码: public static void getType(Object object) { int length = object.getClass().getName().lastIndexOf("."); String type = object.getClass().getName().substring(length + 1); System.out.println(type); } 基本类型不能得到
自己写了个工具类,大家给点意见:可以查看类的详细信息的。 package mycom;import java.lang.reflect.*;/** * the tools of looking for the properties of a class * print the constructor and method * * @author Roy * @since 2007-09-10 * @version 1.0 */ public class GetProperties { /** * return the parameters of the specified methods,constructors or fields fit * the jdk1.4 * * @param acceObject AccessibleObject * @return * @author Roy */ private static String getParameter(AccessibleObject acceObject) { String result = ""; Class[] parameters = null; if (acceObject instanceof Method) { parameters = ((Method) acceObject).getParameterTypes(); } if (acceObject instanceof Constructor) { parameters = ((Constructor) acceObject).getParameterTypes(); } int paramCount = parameters.length; // if no parameters if (paramCount < 1) { return result; } StringBuffer str = new StringBuffer(); for (int i = 0; i < paramCount; i++) { String s = parameters[i].getName();// generally,the s is contain str.append(getShortName(s)).append(","); } if (str.length() > 1)//delete the last "," result = str.substring(0, str.length() - 1); return result; } /** * print the field * @param object Class */ public static void printFields(Class object){ System.out .println("----------------------Fileds----------------------"); StringBuffer str = new StringBuffer(); Field[] field = object.getDeclaredFields();
for(int i=0;i<field.length; i++){ int type =field[i].getModifiers(); str.append(Modifier.toString(type)).append(" ");
Class declared =field[i].getType(); str.append(getShortName(declared.getName())).append(" "); str.append(field[i].getName()).append("\n"); //str.append(field[i].get(field.[i]).toString()).append("\n");//System.out.println(field[i].toString());
} System.out.print(str.toString()); } /** * print the construct function and its parameters * * @param object Class * @return */ public static void printConstructs(Class object) { System.out .println("----------------------Constructors----------------------"); Constructor[] cos = object.getConstructors(); for (int i = 0; i < cos.length; i++) { System.out.print(cos[i].getName() + "("); System.out.print(getParameter(cos[i])); System.out.print(")" + "\n"); } } /** * print the declared method and its parameter * * @param object Class */ public static void printMethods(Class object) { System.out .println("----------------------Methods----------------------"); StringBuffer str = new StringBuffer(); Method[] methods = object.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { int type = methods[i].getModifiers();// Returns the Java language // modifiers // The Modifier class should be used to decode the modifiers if(type != 0) str.append(Modifier.toString(type)).append(" ");
str.append(getShortName(methods[i].getReturnType().getName())) .append(" "); str.append(methods[i].getName()).append("(").append( getParameter(methods[i])); str.append(")").append("\n"); } System.out.println(str); } /** * if the return type is a array,the str is "[*", for example the * function" int[] getNumber(){..}" the * "methods[i].getReturnType().getName()" equals "[I" * * @param str String */ private static String getShortName(String str) { StringBuffer result = new StringBuffer(); int i = -50; if (str.indexOf("[") != -1) {//array type if (str.length() == 2) { switch (str.charAt(1)) { case 'B': result.append("byte[]"); break; case 'S': result.append("short[]"); break; case 'I': result.append("int[]"); break; case 'C': result.append("char[]"); break; case 'J': result.append("long[]"); break; case 'F': result.append("float[]"); break; case 'D': result.append("double[]"); break; } } else { if ((i = str.lastIndexOf(".")) != -1) {//because the end of the str is ";" result.append(str.substring(i + 1,str.length()-1)).append("[]"); } } } else {//no array type if ((i = str.lastIndexOf(".")) != -1) { return str.substring(i + 1); } return str; } return result.toString(); } public static void main(String[] args) throws Exception { if(args.length<1){ StringBuffer str=new StringBuffer(); str.append("usage:java GetProperties ARGS").append("\n"); str.append(" the args contain the package:").append("\n"); str.append(" example: java GetProperties java.lang.String"); System.out.println(str.toString()); System.exit(0); }
{
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 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";
}
}
-------------------------------------
支持,写得够详细的.
只用Object做参数你怎么区分基础类型和包装类?
obj.getClass().toString() ===〉 class java.lang.Integer
int length= object.getClass().getClassName().lastIndexOf(".");
String type = object.getClass().getClassName().subSstring(length+1);
System.out.prinfln(type);
}
public static void getType(Object object) {
int length = object.getClass().getName().lastIndexOf(".");
String type = object.getClass().getName().substring(length + 1);
System.out.println(type);
}
基本类型不能得到
package mycom;import java.lang.reflect.*;/**
* the tools of looking for the properties of a class
* print the constructor and method
*
* @author Roy
* @since 2007-09-10
* @version 1.0
*/
public class GetProperties {
/**
* return the parameters of the specified methods,constructors or fields fit
* the jdk1.4
*
* @param acceObject AccessibleObject
* @return
* @author Roy
*/
private static String getParameter(AccessibleObject acceObject) {
String result = "";
Class[] parameters = null;
if (acceObject instanceof Method) {
parameters = ((Method) acceObject).getParameterTypes();
}
if (acceObject instanceof Constructor) {
parameters = ((Constructor) acceObject).getParameterTypes();
}
int paramCount = parameters.length;
// if no parameters
if (paramCount < 1) {
return result;
}
StringBuffer str = new StringBuffer(); for (int i = 0; i < paramCount; i++) {
String s = parameters[i].getName();// generally,the s is contain str.append(getShortName(s)).append(",");
}
if (str.length() > 1)//delete the last ","
result = str.substring(0, str.length() - 1); return result;
}
/**
* print the field
* @param object Class
*/
public static void printFields(Class object){
System.out
.println("----------------------Fileds----------------------");
StringBuffer str = new StringBuffer();
Field[] field = object.getDeclaredFields();
for(int i=0;i<field.length; i++){
int type =field[i].getModifiers();
str.append(Modifier.toString(type)).append(" ");
Class declared =field[i].getType();
str.append(getShortName(declared.getName())).append(" ");
str.append(field[i].getName()).append("\n");
//str.append(field[i].get(field.[i]).toString()).append("\n");//System.out.println(field[i].toString());
}
System.out.print(str.toString());
} /**
* print the construct function and its parameters
*
* @param object Class
* @return
*/
public static void printConstructs(Class object) {
System.out
.println("----------------------Constructors----------------------"); Constructor[] cos = object.getConstructors();
for (int i = 0; i < cos.length; i++) {
System.out.print(cos[i].getName() + "(");
System.out.print(getParameter(cos[i]));
System.out.print(")" + "\n"); }
} /**
* print the declared method and its parameter
*
* @param object Class
*/
public static void printMethods(Class object) {
System.out
.println("----------------------Methods----------------------");
StringBuffer str = new StringBuffer();
Method[] methods = object.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
int type = methods[i].getModifiers();// Returns the Java language
// modifiers
// The Modifier class should be used to decode the modifiers
if(type != 0)
str.append(Modifier.toString(type)).append(" ");
str.append(getShortName(methods[i].getReturnType().getName()))
.append(" ");
str.append(methods[i].getName()).append("(").append(
getParameter(methods[i]));
str.append(")").append("\n"); }
System.out.println(str);
} /**
* if the return type is a array,the str is "[*", for example the
* function" int[] getNumber(){..}" the
* "methods[i].getReturnType().getName()" equals "[I"
*
* @param str String
*/
private static String getShortName(String str) { StringBuffer result = new StringBuffer();
int i = -50;
if (str.indexOf("[") != -1) {//array type
if (str.length() == 2) {
switch (str.charAt(1)) {
case 'B':
result.append("byte[]");
break;
case 'S':
result.append("short[]");
break;
case 'I':
result.append("int[]");
break;
case 'C':
result.append("char[]");
break;
case 'J':
result.append("long[]");
break;
case 'F':
result.append("float[]");
break;
case 'D':
result.append("double[]");
break;
}
} else {
if ((i = str.lastIndexOf(".")) != -1) {//because the end of the str is ";"
result.append(str.substring(i + 1,str.length()-1)).append("[]");
}
}
} else {//no array type
if ((i = str.lastIndexOf(".")) != -1) {
return str.substring(i + 1);
}
return str;
}
return result.toString();
} public static void main(String[] args) throws Exception {
if(args.length<1){
StringBuffer str=new StringBuffer();
str.append("usage:java GetProperties ARGS").append("\n");
str.append(" the args contain the package:").append("\n");
str.append(" example: java GetProperties java.lang.String");
System.out.println(str.toString());
System.exit(0);
}
//test:
//Class object = Class.forName("java.lang.reflect.Modifier"); Class object = Class.forName(args[0]);
printFields(object);
printConstructs(object);
printMethods(object);
}
}
Java又不是像JavaScript那样的若类型语言,1楼的做法是用JDK5把基本数据类型和对象混淆了。