一、代码一
import com.alanchen.PersonServiceImpl;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Client {
public static void main(String[] args) {
Class<PersonServiceImpl> serviceClass = PersonServiceImpl.class;
//类名:不包括包名。simpleName=PersonServiceImpl
String simpleName = serviceClass.getSimpleName();
//全路径名称:包括包名。name=com.alanchen.PersonServiceImpl
String name = serviceClass.getName();
//该类实现的接口的名字。interfaceName=IPersonService
String interfaceName = serviceClass.getInterfaces()[0].getSimpleName();
//返回public属性。fieldStr=sex
String fieldStr = "";
for (Field item : serviceClass.getFields()) {
fieldStr += item.getName() + ";";
}
//返回所有属性,包括private修饰的属性。declaredFieldStr=personName;age;sex;
String declaredFieldStr = "";
for (Field item : serviceClass.getDeclaredFields()) {
declaredFieldStr += item.getName() + ";";
//设置强制访问(包括private)
item.setAccessible(true);
}
//返回public方法,包括父类Object中public的方法。
//methodStr=personSay;wait;wait;wait;equals;toString;hashCode;getClass;notify;notifyAll;
String methodStr = "";
for (Method item : serviceClass.getMethods()) {
methodStr += item.getName() + ";";
}
//返回该类中的所有方法,包括private方法,不包括父类Object中的方法。
//declaredMethodStr=personSay;personWork;
String declaredMethodStr = "";
for (Method item : serviceClass.getDeclaredMethods()) {
declaredMethodStr += item.getName() + ";";
}
System.out.println("simpleName=" + simpleName);
System.out.println("name=" + name);
System.out.println("interfaceName=" + interfaceName);
System.out.println("fieldStr=" + fieldStr);
System.out.println("declaredFieldStr=" + declaredFieldStr);
System.out.println("methodStr=" + methodStr);
System.out.println("declaredMethodStr=" + declaredMethodStr);
}
}
网友评论