使用java反射技术就可以
public static void main(String[] args) {
Test test = new Test();
System.out.println(test.getClass().getSimpleName());
}
}
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法
利用反射可以完成很多很多功能
获取方法名
public class Test {
public void invoke(){
}
public static void main(String[] args) {
Test test = new Test();
Class clazz = test.getClass();
Method[] methods = clazz.getDeclaredMethods();
for(Method method:methods){
System.out.println(method.getName());
}
}
}
public static void main(String[] args)(
//例如int
int i = 1;
String type = getTypeFromObj(i);
System.out.println(type);//打印出int值
)
public staic String getTypeFromObj(Object o){
//得到Object的所有属性,第一个就是类型type
Field[] fileds = o.getClass().getDeclaredFields();
String type = null;
int i;
for(i=0;itype = fields[i].getType().toString();
break;
}
return type;
}
如下:
public static String do_post(String url, Listname_value_pair) throws IOException {
String body = "{}";
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httpost = new HttpPost(url);
httpost.setEntity(new UrlEncodedFormEntity(name_value_pair, StandardCharsets.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
return body;
}
public static String do_get(String url) throws ClientProtocolException, IOException {
String body = "{}";
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
return body;
}
obj.getClass().getName() 就能显示包名和类名
用反射。。不然就只能强转换挨个试了