继承一组批注和一组方法的最佳方法?

时间:2019-06-12 11:17:56

标签: java inheritance annotations

我想在多个类中使用特定的注释。我知道我可以在春季通过定义新注释来做到这一点:

@Retention(RetentionPolicy.RUNTIME)
@annotation1
@annotation2
@...
public @interface MyCustomAnnotation{}

然后用@MyCustomAnnotation注释我的课程

我还希望这些类具有一些特定的方法,这些方法可以通过简单继承来实现。

现在,由于我想同时为同一类做两件事,所以我想知道是否可以同时完成这两项工作,例如在定义一个具有要继承的方法的类的同时,它也具有注释,因此注释将是继承了它。

我一直在玩@Inherited,但是没有运气。甚至尝试使用类似的东西

@Retention(RetentionPolicy.RUNTIME)
@Inherited
@annotation1
public @interface MyCustomAnnotation{}

@MyCustomAnnotation
public class X {... methods here ...}

public class Y extends X { ... }

这将迫使我为注释使用一个@interface并为我的方法使用一个类,但是以某种方式,该方法也无法正常工作(Y未被注释1注释)

我在做什么错?实现这一目标的最简单方法是什么?

0 个答案:

没有答案
相关问题