public class MethodTest {
public static void main(String[] args) {
try {
System.out.println("调用math类的静态方cos()");
Method cos = Math.class.getDeclaredMethod("cos", Double.TYPE);
Double cos1 = (Double)cos.invoke(null,new Integer(1));
System.out.println("1的正弦值为"+cos1);
System.out.println("调用String类的静态方法equals()");
Method equals = String.class.getDeclaredMethod("equals", Object.class);
Boolean bStrEquals = (Boolean)equals.invoke(new String("我爱我家"), "我爱我家");
System.out.println("字符串是不是我爱我家"+bStrEquals);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
System.out.println("调用math类的静态方cos()");
Method cos = Math.class.getDeclaredMethod("cos", Double.TYPE);
Double cos1 = (Double)cos.invoke(null,new Integer(1));
System.out.println("1的正弦值为"+cos1);
System.out.println("调用String类的静态方法equals()");
Method equals = String.class.getDeclaredMethod("equals", Object.class);
Boolean bStrEquals = (Boolean)equals.invoke(new String("我爱我家"), "我爱我家");
System.out.println("字符串是不是我爱我家"+bStrEquals);
} catch (Exception e) {
e.printStackTrace();
}
}
}