Java:((TestClass)null).testMethod();

publicclass TestClass {
   privatestatic void testMethod(){
        System.out.println("testMethod");
   }
   publicstatic void main(String[] args) {
        ((TestClass)null).testMethod();
   }
}


这个是可以正常运行的,如果把static去掉就会空指针异常
1: null可以转化为任何类型
2: private 只是权限声明
2:static静态关键字 仅仅意味着可以不用实例化这个类 
   通过类名.方法名就可以访问 
   当然也可以通过实例化类的对象后 通过对象.方法名 
   但是不能通过this关键字,因为this是指本实例中的方法被static声明的方法属于类的方法

题目来源:牛客网http://www.nowcoder.com/questionTerminal/e252668ee94947bea07edd7300340115
### 回答1: public void getAnnotation(Class clazz) { Annotation[] annotations = clazz.getAnnotations(); for (Annotation annotation : annotations) { System.out.println("注解名称:" + annotation.annotationType().getSimpleName()); } } ### 回答2: Java的反射机制可以用来获取类、方法、属性等的注解信息。下面是一个简单的示例代码,用于演示如何使用反射获取注解: ```java import java.lang.annotation.Annotation; import java.lang.reflect.Method; @MyAnnotation(name = "TestClass") public class TestClass { @MyAnnotation(name = "TestMethod") public void testMethod() { System.out.println("This is a test method"); } public static void main(String[] args) { Class<TestClass> clazz = TestClass.class; Annotation[] classAnnotations = clazz.getAnnotations(); for (Annotation annotation : classAnnotations) { if (annotation instanceof MyAnnotation) { MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println("Class Annotation Name: " + myAnnotation.name()); } } Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { Annotation[] methodAnnotations = method.getAnnotations(); for (Annotation annotation : methodAnnotations) { if (annotation instanceof MyAnnotation) { MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println("Method Annotation Name: " + myAnnotation.name()); } } } } } @interface MyAnnotation { String name(); } ``` 在上面的代码中,我们首先定义了一个注解 `@MyAnnotation`,并将其分别应用到 `TestClass` 类和 `testMethod` 方法上。然后,通过反射获取 `TestClass` 类的注解和 `testMethod` 方法的注解,并将其输出到控制台上。 从输出结果来看,我们可以得到类注解的名称为 "TestClass",方法注解的名称为 "TestMethod"。 注意,以上只是一个简单的示例,实际使用反射获取注解时,可能需要结合具体的业务逻辑进行处理。 ### 回答3: 反射是Java中强大的特性之一,可以在运行时动态获取类的信息,并且可以通过反射机制获取类、方法、字段等的注解信息。 下面是一个示例代码,用于演示如何使用Java反射获取注解: ```java import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; // 定义一个注解 @interface MyAnnotation { String value() default ""; } // 定义一个类并添加注解 @MyAnnotation(value = "这是一个自定义注解") class MyClass { @MyAnnotation(value = "这是一个字段注解") private String myField; @MyAnnotation(value = "这是一个方法注解") public void myMethod() { System.out.println("Hello, Reflection!"); } } public class ReflectionDemo { public static void main(String[] args) { // 获取类注解 Class<MyClass> clazz = MyClass.class; MyAnnotation classAnnotation = clazz.getAnnotation(MyAnnotation.class); if (classAnnotation != null) { System.out.println("类注解值:" + classAnnotation.value()); } // 获取字段注解 try { Field field = clazz.getDeclaredField("myField"); MyAnnotation fieldAnnotation = field.getAnnotation(MyAnnotation.class); if (fieldAnnotation != null) { System.out.println("字段注解值:" + fieldAnnotation.value()); } } catch (NoSuchFieldException e) { e.printStackTrace(); } // 获取方法注解 try { Method method = clazz.getMethod("myMethod"); MyAnnotation methodAnnotation = method.getAnnotation(MyAnnotation.class); if (methodAnnotation != null) { System.out.println("方法注解值:" + methodAnnotation.value()); } } catch (NoSuchMethodException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们首先定义了一个自定义注解`MyAnnotation`,并在`MyClass`类的类声明、字段和方法上添加了该注解。然后使用反射机制获取类注解、字段注解和方法注解,并从中获取注解的值打印输出。 运行上述代码,输出结果如下: ``` 类注解值:这是一个自定义注解 字段注解值:这是一个字段注解 方法注解值:这是一个方法注解 ``` 以上就是使用Java反射获取注解的示例代码。通过这种方式,我们可以在运行时动态地获取类的注解信息,为程序的灵活性和扩展性提供了很大的便利。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值