题目内容

1、以下代码的运行结果(1)Perform.javaimport java.lang.annotation.*;@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Perform{}(2)Test.javaimport java.lang.annotation.*;import java.lang.reflect.Method;public class Test {@Performpublic void testMethod() {}@Performpublic void test() {}public static void main(String[] args) throws Exception {Method testMethod = Test.class.getDeclaredMethod("testMethod");System.out.println(testMethod);Annotation[] ans = testMethod.getDeclaredAnnotations();if( testMethod != null ) {System.out.println(ans.length);}else {System.out.println("无注解");}}}

A. 程序报错
B. 无注解
C. 2
D. 1

查看答案
更多问题

4、下列关于反射和泛型的联系说法正确的是()。

A. 泛型只在编译时有效,无法在运行期获取泛型的具体类型
B. 反射机制可以获取泛型的具体类型
C. 可以通过反射绕过泛型检查,因为运行期泛型根本没有用
D. 以上说法都不对

7、假定Tester类有如下test方法:public int test( int p1, Integer p2)以下哪段代码能正确编译并动态调用一个Tester对象的test方法()?

A. Class classType = Tester.class;Object tester = classType.newInstance();Method addMethod = classType.getMethod( "test", int.class, int.class);Object result = addMethod.invoke( tester, 100, 200 );
B. Class classType = Tester.class;Object tester = classType.newInstance();Method addMethod = classType.getMethod( "test", int.class, int.class);int result = addMethod.invoke( tester, 100, 200 );
Class classType = Tester.class;Object tester = classType.newInstance();Method addMethod = classType.getMethod( "test", int.class, Integer.class);Object result = addMethod.invoke( tester, 100, 200 );
D. Class classType = Tester.class;Object tester = classType.newInstance();Method addMethod = classType.getMethod( "test", int.class, Integer.class);Integer result = addMethod.invoke( tester, 100, 200 );

8、以下代码运行的结果是()。import java.lang.reflect.*;public class Person {public String eat(Integer num, String food ) {System.out.print("吃" + num + "个" + food + " ");return food;}public static void main(String[] args) throws Exception {Class clazz = Person.class;Person p = (Person) clazz.newInstance();Method method = clazz.getMethod("eat", Integer.class, String.class);Class type = method.getReturnType();method.invoke(p, 1, "苹果");System.out.print(type.getSimpleName() + " ");System.out.print(method.getGenericParameterTypes()[1] instanceof Class);}}

A. 吃1个苹果 java.lang.String true
B. 吃1个苹果 String true
C. 吃1个苹果 java.lang.String false
D. 吃1个苹果 String false

3、(多选题)假定Tester类:Class classType = Tester.class;Constructor constructor = classType.getConstructor();以下能正确构造一个Tester对象的是()。

A. Class.newInstance();
B. constructor.newInstance();
C. Tester.newInstance();
D. classType.newInstance();

答案查题题库