package cclass;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class He {
public String show() {
return "是个人";
}
public String show(String name) {
return name + "是个人";
}
public String friend(String name, String meFriend) {
return meFriend + "是" + name + "的朋友";
}
public String friend(String name, String meFriend, String[] friend) {
StringBuffer string = new StringBuffer(meFriend + "是" + name + "的朋友" + name + "还有一些朋友比如:");
for (String str : friend) {
string.append(str + "\t");
}
return string.toString();
}
}
public class cclass {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException,
SecurityException, IllegalArgumentException, InvocationTargetException {
Class<He> he = He.class;
He hh = he.newInstance();
Method[] mods = he.getMethods();
System.out.println("一下打印He类的所有方法");
for (Method mod : mods) {
System.out.println(mod);
}
Method show = he.getMethod("show", String.class);
String str = (String) show.invoke(hh, "aksjxajk");
System.out.println(str);
Method friend = he.getMethod("friend", String.class, String.class);
str = (String) friend.invoke(hh, "aksjxajk", "dcsdcs");
System.out.println(str);
Method friends = he.getMethod("friend", String.class, String.class, String[].class);
str = (String) friends.invoke(hh, "aksjxajk", "dcsdcs", new String[] { "asxasxa", "asxasx", "asxasx" });
System.out.println(str);
Method show3 = he.getMethod("show", new Class[] { String.class });
String str3 = (String) show3.invoke(hh, new Object[] { "aksjxajk" });
System.out.println(str3);
Method friend3 = he.getMethod("friend", new Class[] { String.class, String.class });
str3 = (String) friend3.invoke(hh, new Object[] { "aksjxajk", "dcsdcs" });
System.out.println(str3);
Method friends3 = he.getMethod("friend", new Class[] { String.class, String.class, String[].class });
str3 = (String) friends3.invoke(hh,
new Object[] { "aksjxajk", "dcsdcs", new String[] { "asxasxa", "asxasx", "asxasx" } });
System.out.println(str3);
}
}