子类是否可以继承父类的注解
来源:互联网 发布:mysql 用户管理 编辑:程序博客网 时间:2024/06/05 19:08
本文转载于:http://elf8848.iteye.com/blog/1621392
不了解注解基础知识的请先看《JDK 5 Annotation\注解\注释\自定义注解》
子类可以继承到父类上的注解吗?
—————————————————————–
我们知道在编写自定义注解时,可以通过指定@Inherited注解,指明自定义注解是否可以被继承。但实现情况又可细分为多种。
测试环境如下:
—————————————————————–
父类的类上和方法上有自定义的注解–MyAnnotation
子类继承了这个父类,分别:
子类方法,实现了父类上的抽象方法
子类方法,继承了父类上的方法
子类方法,覆盖了父类上的方法
MyAnnotation自定义注解
—————————————————————–
- package test.annotation;
- import java.lang.annotation.Inherited;
- import java.lang.annotation.Retention;
- /**
- * 自定义注解
- */
- //@Inherited //可以被继承
- @Retention(java.lang.annotation.RetentionPolicy.RUNTIME) //可以通过反射读取注解
- public @interface MyAnnotation {
- String value();
- }
package test.annotation;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;/** * 自定义注解 *///@Inherited //可以被继承@Retention(java.lang.annotation.RetentionPolicy.RUNTIME) //可以通过反射读取注解public @interface MyAnnotation { String value(); }
父类
—————————————————————–
- package test.annotation;
- @MyAnnotation(value = “类名上的注解”)
- public abstract class ParentClass {
- @MyAnnotation(value = “父类的abstractMethod方法”)
- public abstract void abstractMethod();
- @MyAnnotation(value = “父类的doExtends方法”)
- public void doExtends() {
- System.out.println(” ParentClass doExtends …”);
- }
- @MyAnnotation(value = “父类的doHandle方法”)
- public void doHandle(){
- System.out.println(” ParentClass doHandle …”);
- }
- }
package test.annotation;@MyAnnotation(value = "类名上的注解")public abstract class ParentClass { @MyAnnotation(value = "父类的abstractMethod方法") public abstract void abstractMethod(); @MyAnnotation(value = "父类的doExtends方法") public void doExtends() { System.out.println(" ParentClass doExtends ..."); } @MyAnnotation(value = "父类的doHandle方法") public void doHandle(){ System.out.println(" ParentClass doHandle ..."); }}
子类
—————————————————————–
- package test.annotation;
- public class SubClass extends ParentClass{
- //子类实现父类的抽象方法
- @Override
- public void abstractMethod() {
- System.out.println(”子类实现父类的abstractMethod抽象方法”);
- }
- //子类继承父类的doExtends方法
- //子类覆盖父类的doHandle方法
- @Override
- public void doHandle(){
- System.out.println(”子类覆盖父类的doHandle方法”);
- }
- }
package test.annotation;public class SubClass extends ParentClass{ //子类实现父类的抽象方法 @Override public void abstractMethod() { System.out.println("子类实现父类的abstractMethod抽象方法"); } //子类继承父类的doExtends方法 //子类覆盖父类的doHandle方法 @Override public void doHandle(){ System.out.println("子类覆盖父类的doHandle方法"); }}
测试类
—————————————————————–
- package test.annotation;
- import java.lang.reflect.Method;
- public class MainTest {
- public static void main(String[] args) throws SecurityException,
- NoSuchMethodException {
- Class<SubClass> clazz = SubClass.class;
- if (clazz.isAnnotationPresent(MyAnnotation.class)) {
- MyAnnotation cla = clazz
- .getAnnotation(MyAnnotation.class);
- System.out.println(”子类继承到父类类上Annotation,其信息如下:”+cla.value());
- } else {
- System.out.println(”子类没有继承到父类类上Annotation”);
- }
- // 实现抽象方法测试
- Method method = clazz.getMethod(”abstractMethod”, new Class[] {});
- if (method.isAnnotationPresent(MyAnnotation.class)) {
- MyAnnotation ma = method
- .getAnnotation(MyAnnotation.class);
- System.out.println(”子类实现父类的abstractMethod抽象方法,继承到父类抽象方法中的Annotation,其信息如下:”+ma.value());
- } else {
- System.out.println(”子类实现父类的abstractMethod抽象方法,没有继承到父类抽象方法中的Annotation”);
- }
- //覆盖测试
- Method methodOverride = clazz.getMethod(”doExtends”, new Class[] {});
- if (methodOverride.isAnnotationPresent(MyAnnotation.class)) {
- MyAnnotation ma = methodOverride
- .getAnnotation(MyAnnotation.class);
- System.out
- .println(”子类继承父类的doExtends方法,继承到父类doExtends方法中的Annotation,其信息如下:”+ma.value());
- } else {
- System.out.println(”子类继承父类的doExtends方法,没有继承到父类doExtends方法中的Annotation”);
- }
- //继承测试
- Method method3 = clazz.getMethod(”doHandle”, new Class[] {});
- if (method3.isAnnotationPresent(MyAnnotation.class)) {
- MyAnnotation ma = method3
- .getAnnotation(MyAnnotation.class);
- System.out
- .println(”子类覆盖父类的doHandle方法,继承到父类doHandle方法中的Annotation,其信息如下:”+ma.value());
- } else {
- System.out.println(”子类覆盖父类的doHandle方法,没有继承到父类doHandle方法中的Annotation”);
- }
- }
- }
package test.annotation;import java.lang.reflect.Method;public class MainTest { public static void main(String[] args) throws SecurityException, NoSuchMethodException { Class<SubClass> clazz = SubClass.class; if (clazz.isAnnotationPresent(MyAnnotation.class)) { MyAnnotation cla = clazz .getAnnotation(MyAnnotation.class); System.out.println("子类继承到父类类上Annotation,其信息如下:"+cla.value()); } else { System.out.println("子类没有继承到父类类上Annotation"); } // 实现抽象方法测试 Method method = clazz.getMethod("abstractMethod", new Class[] {}); if (method.isAnnotationPresent(MyAnnotation.class)) { MyAnnotation ma = method .getAnnotation(MyAnnotation.class); System.out.println("子类实现父类的abstractMethod抽象方法,继承到父类抽象方法中的Annotation,其信息如下:"+ma.value()); } else { System.out.println("子类实现父类的abstractMethod抽象方法,没有继承到父类抽象方法中的Annotation"); } //覆盖测试 Method methodOverride = clazz.getMethod("doExtends", new Class[] {}); if (methodOverride.isAnnotationPresent(MyAnnotation.class)) { MyAnnotation ma = methodOverride .getAnnotation(MyAnnotation.class); System.out .println("子类继承父类的doExtends方法,继承到父类doExtends方法中的Annotation,其信息如下:"+ma.value()); } else { System.out.println("子类继承父类的doExtends方法,没有继承到父类doExtends方法中的Annotation"); } //继承测试 Method method3 = clazz.getMethod("doHandle", new Class[] {}); if (method3.isAnnotationPresent(MyAnnotation.class)) { MyAnnotation ma = method3 .getAnnotation(MyAnnotation.class); System.out .println("子类覆盖父类的doHandle方法,继承到父类doHandle方法中的Annotation,其信息如下:"+ma.value()); } else { System.out.println("子类覆盖父类的doHandle方法,没有继承到父类doHandle方法中的Annotation"); } }}
编写自定义注解时未写@Inherited的运行结果
—————————————————————–
子类没有继承到父类类上Annotation
子类实现父类的abstractMethod抽象方法,没有继承到父类抽象方法中的Annotation
子类继承父类的doExtends方法,继承到父类doExtends方法中的Annotation,其信息如下:父类的doExtends方法
子类覆盖父类的doHandle方法,没有继承到父类doHandle方法中的Annotation
编写自定义注解时写了@Inherited的运行结果
—————————————————————–
子类继承到父类类上Annotation,其信息如下:类名上的注解
子类实现父类的abstractMethod抽象方法,没有继承到父类抽象方法中的Annotation
子类继承父类的doExtends方法,继承到父类doExtends方法中的Annotation,其信息如下:父类的doExtends方法
子类覆盖父类的doHandle方法,没有继承到父类doHandle方法中的Annotation
结论
—————————————————————–
父类的类上和方法上有自定义的注解,
子类继承了这个父类,的情况下。
编写自定义注解时未写@Inherited的运行结果:编写自定义注解时写了@Inherited的运行结果:子类的类上能否继承到父类的类上的注解?否能子类方法,实现了父类上的抽象方法,这个方法能否继承到注解?否否子类方法,继承了父类上的方法,这个方法能否继承到注解?能能子类方法,覆盖了父类上的方法,这个方法能否继承到注解?否否
我们知道在编写自定义注解时,可以通过指定@Inherited注解,指明自定义注解是否可以被继承。
通过测试结果来看,@Inherited 只是可控制 对类名上注解是否可以被继承。不能控制方法上的注解是否可以被继承。
附注
—————————————————————–
Spring 实现事务的注解@Transactional 是可以被继承的,
通过查看它的源码可以看到@Inherited。
- 子类是否可以继承父类的注解
- JAVA中子类是否可以可以继承、重写父类的静态方法--总结
- 子类是否可以继承父类的私有属性和方法?
- 子类可以继承到父类上的注解吗--有结论了
- 子类可以继承到父类上的注解吗--有结论了
- 子类可以继承到父类上的注解吗--有结论了
- 子类可以继承到父类上的注解吗--有结论了
- 子类可以继承到父类上的注解吗--有结论了
- 子类可以继承到父类上的注解吗--有结论了
- Java中子类是否可以继承父类的static变量和方法而呈现多态特性
- Java中子类是否可以继承父类的static变量和方法而呈现多态特性
- 03-子类可以继承父类的什么?
- 子类是否继承父类的 annotation - Inherited
- java 子类可以继承父类中的什么,子类中方法覆盖,final关键字的含义
- 【Java笔记】继承父类的方法可以调用子类的方法(子类必须在重写相应方法)
- Java编程中,子类可以用用这种方法给继承父类的成员变量赋值么?
- java中instanceof关键字的使用,以及子类是否继承了父类的所有属性
- 当两个类构成继承关系时,子类是否必须重写父类的所有方法?
- HBase源码分析之regionserver写入流程分析
- MVP
- 用Ant实现Java项目的自动构建和部署
- c语言:输出500-600以内的所有素数,还是纠错的一个程序,大家看看
- HDU 2046 骨牌铺方格 简单递推
- 子类是否可以继承父类的注解
- HTML之简介、文档类型、元素、行内元素
- jmap 几个慎用操作
- SSM + Android 网络交互的那些事
- HTML+CSS初步 (1)
- Leetcode 38 Count and Say
- 单一职责原则(设计模式_03)
- Android 颜色渲染 PorterDuff及Xfermode详解
- 数据结构与算法笔记 lesson 20 二叉树遍历