1 //gets the properties of the corresponding run-time class2 @Test3 Public voidtest1 () {4Class clazz = person.class;5 //1.getFields (): Can only get properties declared as public in the runtime class and its parent class6field[] Fields =clazz.getfields ();7 8 for(inti =0; I < fields.length;i++){9System. out. println (Fields[i]);Ten } OneSystem. out. println (); A //2.getDeclaredFields (): Gets all the properties declared by the runtime class itself -field[] Fields1 =clazz.getdeclaredfields (); - for(Field f:fields1) { theSystem. out. println (F.getname ()); - } -}
1 //permission modifier variable type variable name2 //gets the contents of the various parts of the property3 @Test4 Public voidtest2 () {5Class clazz = person.class;6field[] Fields1 =clazz.getdeclaredfields ();7 for(Field f:fields1) {8 //1. Get permission modifiers for each property9 inti =f.getmodifiers ();TenString str1 =modifier.tostring (i); OneSystem. out. Print (str1 +" "); A //2. Get the type of the property -Class type =F.gettype (); -System. out. Print (Type.getname () +" "); the //3. Get the property name -System. out. Print (F.getname ()); - -System. out. println (); + } -}
1 //calling properties specified in the run-time class2 @Test3 Public voidTest3 () throws exception{4Class clazz = person.class;5 //1. Get the specified property6 //GetField (String fieldName): Gets the property named FieldName of the specified property declared as public in the run-time class7Field name = Clazz.getfield ("name");8 //2. Creating objects for the runtime class9Person p =(person) clazz.newinstance ();TenSystem. out. println (p); One //3. Assign a value to the specified property of the run-time class AName.Set(P,"Jerry"); -System. out. println (p); -System. out. println ("%"+name.Get(P)); the -System. out. println (); - //Getdeclaredfield (String fieldName): Gets the property named FieldName specified in the run-time class -Field age = Clazz.getdeclaredfield (" Age"); + //because of the restriction of the property permission modifier, in order to ensure that the property can be assigned a value, it is necessary to make this property operational before the operation. -Age.setaccessible (true); +Age.Set(P,Ten); ASystem. out. println (p); at - //Field id = clazz.getfield ("id"); - -}
1 classPerson extends creature<string>implements comparable,myinterface{2 PublicString name;3 Private intAge ;4 intID;5 //When you create a class, try to keep an empty parameter constructor. 6 PublicPerson () {7 super ();8 //System.out.println ("The weather is very sultry today");9 }Ten PublicPerson (String name) { One super (); A This. Name =name; - } - PrivatePerson (String name,intAge ) { the super (); - This. Name =name; - This. Age =Age ; - } + PublicString GetName () { - returnname; + } A Public voidsetName (String name) { at This. Name =name; - } - Public intGetage () { - returnAge ; - } - Public voidSetage (intAge ) { in This. Age =Age ; - } to + Public intgetId () { - returnID; the } * Public voidSetId (intID) { $ This. ID =ID;Panax Notoginseng } -@MyAnnotation (value ="abc123") the Public voidShow () { +System. out. println ("I am a person! "); A } the + PrivateInteger Display (String Nation,integer i) throws exception{ -System. out. println ("My nationality is:"+nation); $ returni; $ } - @Override - PublicString toString () { the return "Person [Name="+ name +", age="+ Age +"]"; - }Wuyi @Override the Public intcompareTo (Object o) { - //TODO auto-generated Method Stub Wu return 0; - } About $ Public Static voidinfo () { -System. out. println ("The Chinese! "); - } - A classbird{ + the } - $}
ImportStaticJava.lang.annotation.elementtype.constructor;importStaticJava.lang.annotation.elementtype.field;importStaticJava.lang.annotation.elementtype.local_variable;importStaticJava.lang.annotation.elementtype.method;importStaticJava.lang.annotation.elementtype.parameter;importStaticJava.lang.annotation.elementtype.type;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import Java.lang.annotation.Target; @Target ({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, local_variable}) @Retention (retentionpolicy.runtime) Public@interface myannotation {String value ();}
Public class Creature<t>{ publicdouble weight; Public void Breath () { System. out. println (" breathe! "); }}
import java.io.Serializable; Public Interface MyInterface extends serializable{}
Java Learning--reflection properties operations