1 // 1. Get the parent class of the run-time class 2 @Test 3 Public void test1 () {4 Class clazz = person. class ; 5 Class superclass = clazz.getsuperclass (); 6 System. out . println (superclass); 7 }
1 // 2. Get the parent class with generics 2 @Test 3 Public void test2 () {4 Class clazz = person. class ; 5 Type type1 = clazz.getgenericsuperclass (); 6 System. out . println (type1); 7 }
1 //* . Get the generic of the parent class2 @Test3 Public voidtest3 () {4Class clazz = person.class;5Type type1 =Clazz.getgenericsuperclass ();6 7Parameterizedtype param =(Parameterizedtype) type1;8Type[] Ars =param.getactualtypearguments ();9 TenSystem. out. println ((Class) ars[0]). GetName ()); One } A
1 //4. Get the implemented interface2 @Test3 Public voidtest4 () {4Class clazz = person.class;5Class[] Interfaces =clazz.getinterfaces ();6 for(Class i:interfaces) {7System. out. println (i);8 }9}
1 // 5. Get the package you are hosting 2 @Test 3 Public void Test5 () {4 Class clazz = person. class ; 5 Package pack = clazz.getpackage (); 6 System. out . println (pack); 7 }
1 //6. Get Annotations2 @Test3 Public voidTest6 () {4Class clazz = person.class;5annotation[] Anns =clazz.getannotations ();6 for(Annotation A:anns) {7System. out. println (a);8 }9}
1@MyAnnotation (value ="Atguigu")2 Public classPerson extends creature<string>implements comparable,myinterface{3 PublicString name;4 Private intAge ;5 intID;6 //When you create a class, try to keep an empty parameter constructor. 7 PublicPerson () {8 super ();9 //System.out.println ("The weather is very sultry today");Ten } One PublicPerson (String name) { A super (); - This. Name =name; - } the PrivatePerson (String name,intAge ) { - super (); - This. Name =name; - This. Age =Age ; + } - PublicString GetName () { + returnname; A } at Public voidsetName (String name) { - This. Name =name; - } - Public intGetage () { - returnAge ; - } in Public voidSetage (intAge ) { - This. Age =Age ; to } + - Public intgetId () { the returnID; * } $ Public voidSetId (intID) {Panax Notoginseng This. ID =ID; - } the@MyAnnotation (value ="abc123") + Public voidShow () { ASystem. out. println ("I am a person! "); the } + - PrivateInteger Display (String Nation,integer i) throws exception{ $System. out. println ("My nationality is:"+nation); $ returni; - } - @Override the PublicString toString () { - return "Person [Name="+ name +", age="+ Age +"]";Wuyi } the @Override - Public intcompareTo (Object o) { Wu //TODO auto-generated Method Stub - return 0; About } $ - Public Static voidinfo () { -System. out. println ("The Chinese! "); - } A + classbird{ the -}
1ImportStaticJava.lang.annotation.ElementType.CONSTRUCTOR;2ImportStaticJava.lang.annotation.ElementType.FIELD;3ImportStaticJava.lang.annotation.ElementType.LOCAL_VARIABLE;4ImportStaticJava.lang.annotation.ElementType.METHOD;5ImportStaticJava.lang.annotation.ElementType.PARAMETER;6ImportStaticJava.lang.annotation.ElementType.TYPE;7 8 import java.lang.annotation.Retention;9 import Java.lang.annotation.RetentionPolicy;Ten import Java.lang.annotation.Target; One A @Target ({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, local_variable}) - @Retention (retentionpolicy.runtime) - Public@interface myannotation { the String value (); -}
1 public class Creature<t>{ public double weight; 3 Public void breath () { 5 System. out . println ( " breathe! ); 6 7 }
1 import java.io.Serializable; 2 3 public interface MyInterface extends serializable{ 4 5 }
Java Learning--reflection of other operations