package denglianbin.java.reflect;
import java.lang.reflect.Method;
public class ReflectTest10 {
public static void main(String[] args) throws Exception {
//调用一个方法四要素:
//1.对象
//2.login方法名
//3.实参列表
//4.返回值
Class c = Class.forName("denglianbin.java.service.UserService");
Object obj = c.newInstance();
Method loginmethod = c.getDeclaredMethod("login", String.class, String.class);
//obj对象
//logminmethod方法
//"admin","123"实参
//reValue 返回值
Object retValue = loginmethod.invoke(obj, "admin","123");
System.out.println(retValue);
}
}
package denglianbin.java.service;
public class UserService {
public boolean login(String name, String password){
if("admin".equals(name)&&"123".equals(password)){
return true;
}
return false;
}
public void logout(){
System.out.println("系统已经安全退出!");
}
}