資源簡介
Java 動態調用類中的方法(基礎篇-實例146).zip
代碼片段和文件信息
package?com.mingrisoft.reflection;
import?java.lang.reflect.Method;
public?class?MethodTest?{
????public?static?void?main(String[]?args)?{
????????try?{
????????????System.out.println(“調用Math類的靜態方法sin()“);
????????????Method?sin?=?Math.class.getDeclaredMethod(“sin“?Double.TYPE);
????????????Double?sin1?=?(Double)?sin.invoke(null?new?Integer(1));
????????????System.out.println(“1的正弦值是:“?+?sin1);
????????????System.out.println(“調用String類的非靜態方法equals()“);
????????????Method?equals?=?String.class.getDeclaredMethod(“equals“?object.class);
????????????Boolean?mrsoft?=?(Boolean)?equals.invoke(new?String(“明日科技“)?“明日科技“);
????????????System.out.println(“字符串是否是明日科技:“?+?mrsoft);
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????}
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????232??2015-08-28?13:54??.classpath
?????文件?????????379??2015-08-28?13:54??.project
?????文件????????1744??2015-08-28?13:54??bin\com\mingrisoft\reflection\MethodTest.class
?????文件?????????828??2015-08-28?13:54??src\com\mingrisoft\reflection\MethodTest.java
評論
共有 條評論