public static void main(String[] args) {
stdOut.println("");
stdOut.println("Testing interface VHS...");
//VHS vhs;
Class interfaceDescriptor = null;//Class类型的变量
try {
interfaceDescriptor = Class.forName("VHS");//查找接口VHS,并且把它赋给interfaceDescriptor
} catch (ClassNotFoundException cnfe) {
fail("1: Interface VHS not found " +
"(check the name of the interface)");
}
assertTrue("2: VHS is not an Interface",
interfaceDescriptor.isInterface());//检测VHS是否是接口
Method[] methods = interfaceDescriptor.getMethods();//方法数组型的变量
assertTrue("3: VHS should contain two methods",
methods.length == 2);
try {
Method method =
interfaceDescriptor.getMethod("getFormat", new Class[0]);//获取方法
Class returnType = method.getReturnType();//获取返回类型
assertTrue(
"4: the return type of method getFormat is incorrect",
returnType.getName().equals("java.lang.String"));//检测返回类型
} catch (NoSuchMethodException nsme) {
fail("5: Method getFormat not found " +
"(check the name of the method)");
}
try {
Method method =
interfaceDescriptor.getMethod("getLanguage", new Class[0]);
Class returnType = method.getReturnType();
assertTrue(
"6: the return type of method getLanguage is incorrect",
returnType.getName().equals("java.lang.String"));
} catch (NoSuchMethodException nsme) {
fail("7: Method getLanguage not found " +
"(check the name of the method)");
}
stdOut.println("done");
}
测试interface
最新推荐文章于 2023-04-25 20:45:05 发布