Directly speak with the Code. For more information, see annotations ..
The first is the custom annotation using @ inherited ---- inheritedtest. Java
Package COM. jadyer. annotation. inherited; </P> <p> Import Java. lang. annotation. inherited; <br/> Import Java. lang. annotation. retention; <br/> Import Java. lang. annotation. retentionpolicy; </P> <p> @ retention (retentionpolicy. runtime) <br/> @ inherited <br/> Public @ interface inheritedtest {<br/> string value (); <br/>}< br/> // annotation in the default parent class is not inherited into the subclass, if you want the subclass to inherit the annotation in the parent class, <br/> // you can add Java when defining the annotation type. lang. annotation. annotation of the inherited type
Then we use our custom @ inheritedtest annotation to mark the parent class ---- parent. Java
Package COM. jadyer. annotation. inherited; </P> <p> @ inheritedtest ("jadyer") <br/> Public Class Parent {<br/> @ inheritedtest ("javaee ") <br/> Public void dosomething () {<br/> system. out. println ("parent do something"); <br/>}< br/>}
The child. Java subclass inherits the parent. Java class.
Package COM. jadyer. annotation. inherited; </P> <p> public class Child extends parent {<br/> @ override <br/> Public void dosomething () {<br/> system. out. println ("child do something"); <br/>}< br/>}
Finally, the reflection mechanism is used to test whether the subclass inherits the @ inheritedtest annotation ---- test. Java in the parent class.
Package COM. jadyer. annotation. inherited; </P> <p> Import Java. lang. reflect. method; </P> <p>/** <br/> * test whether the Child class inherits the inheritedtest annotation in the parent by reflection. <br/> */<br/> public class test {<br/> Public static void main (string [] ARGs) throws securityexception, nosuchmethodexception {<br/> classtest (); <br/> methodtest (); <br/>}</P> <p>/** <br/> * test whether the subclass inherits the annotation defined on the class in the parent class by reflection. <br/> */<br/> Public static void classte ST () {<br/> class <child> C = child. class; </P> <p> If (C. isannotationpresent (inheritedtest. class) {<br/> inheritedtest = C. getannotation (inheritedtest. class); <br/> string value = inheritedtest. value (); <br/> system. out. println (value ); <br/>}</P> <p>/** <br/> * test whether the subclass inherits the method defined in the parent class by reflection. <br/> */<br/> Public static void methodtest () throws securityexception, nosuchmethodexception {<Br/> class <child> C = child. class; <br/> method = C. getmethod ("dosomething", new class [] {}); </P> <p> If (method. isannotationpresent (inheritedtest. class) {<br/> inheritedtest = method. getannotation (inheritedtest. class); <br/> string value = inheritedtest. value (); <br/> system. out. println (value ); <br/>}< br/>/*********** [run output ]****** ************************************* * ************************/<Br/> // run the output, we will find that only jadyer is output, and no javaee <br/> // if child is commented out. in Java, dosomething () is run again, and jadyer and javaee are output. <br/> // Why? The problem lies in child. java overrides parent. java dosomething () method <br/>/********** [when the annotation in the parent class is marked by @ inherited ]********** *******************************/<br/> // if the annotation of the parent class is defined on the class, sub-classes can be inherited <br/> // If the annotation of the parent class is defined on the method, then the subclass can still inherit <br/> // If the subclass overrides the method that defines the annotation in the parent class, then the subclass cannot inherit the annotation of this method <br/> // that is, when the subclass is rewritten to the method marked by @ inherited in the parent class, this method will be overwritten with the above annotations <br/> // [note] the descriptions of this situation in the JDK document are not comprehensive <br/> // [additional] Interface is unable to inherit the annotation defined in the interface as @ inherited <br/> /****************** **************************************** *******************************/