Sample class
@SuppressWarnings ("Unused") Public classPerson { PublicString Name; Private intAge ; PublicGender Gender; Private StaticString species = "human"; PublicPerson () {Name= "Anonymous"; Age=-1; } PublicPerson (String name) {Name=name; } PrivatePerson (String name,intAge ) {Name=name; Age=Age ; } PrivatePerson (Gender g) {Gender=G; } Public voidRun () {System.out.println (Name+ "Run!"); } Public voidAttack () {System.out.println (Name+ "Hit!"); } Public voidAttack (String name) {System.out.println (name+ "hit" +name+ "!"); } Private voidEat (String food) {System.out.println (Name+ "Eat" +Food ); } Public voidintroduce () {System.out.println ("My name is" +name+ ", I Am" +age+ "year old. "); } Public Static voidPlayGame (String gamename) {System.out.println ("Play" +gamename+ "game"); } Public Static voidMain (string[] args) {System.out.println ("Main"); for(String S:args) System.out.println (s); }}enumgender{Male,female}
Non-parametric constructors for reflection classes:
@Test // non-parametric constructors for reflection classes Public void throws exception{ = Class.forName ("Pro.shaowei.reflect.Person"); Constructor c=clazz.getconstructor (); = (person) c.newinstance (); = (person) clazz.newinstance (); P.introduce (); P.run (); P1. Introduce (); P1. Run ();}
A parameter constructor for a reflection class:
@Test // parametric constructors for reflection classes Public void throws exception{ Class clazz=class.forname ("Pro.shaowei.reflect.Person"); Constructor c=clazz.getconstructor (String. Class); Person P= ((person) c.newinstance ("Zhang San")); P.introduce (); P.run ();}
Private constructors for reflection classes:
@Test//private constructors for reflection classes Public voidConstructor3 ()throwsexception{Class clazz=class.forname ("Pro.shaowei.reflect.Person"); //when you reflect a private constructor, you must use the Getdeclaredconstructor methodConstructor C=clazz.getdeclaredconstructor (String.class,int.class); C.setaccessible (true);//violent reflexesPerson p= (person) c.newinstance ("Zhang San", 25)); P.introduce (); P.run ();}
Public non-parametric methods for reflection classes:
@Test // Public non-parametric method of reflection class Public void throws exception{ Class clazz=class.forname ("Pro.shaowei.reflect.Person"); Person P=(person) clazz.newinstance (); Method=clazz.getmethod ("Run"); Method.invoke (P);}
The public-aware method of the Reflection class:
@Test // Public-owned methods for reflection classes Public void throws exception{ Class clazz=class.forname ("Pro.shaowei.reflect.Person"); Person P=(person) clazz.newinstance (); Method=clazz.getmethod ("Attack", String. Class); Method.invoke (P,"John Doe");}
Private, parameter-aware methods for reflection classes:
@Test // private and parametric methods for reflection classes Public void throws exception{ Class clazz=class.forname ("Pro.shaowei.reflect.Person"); Person P=(person) clazz.newinstance (); Method=clazz.getdeclaredmethod ("Eat", String. Class); Method.setaccessible (true); Method.invoke (P,"banana");}
Static and parametric methods for reflection classes:
@Test // static and Parametric methods for reflection classes Public void throws exception{ Class clazz=class.forname ("Pro.shaowei.reflect.Person"); Person P=(person) clazz.newinstance (); Method=clazz.getdeclaredmethod ("PlayGame", String. Class); Method.setaccessible (true); Method.invoke (P,"minesweeper");}
The main method of the Reflection class:
@Test // The main method of the Reflection class Public void throws exception{ Class clazz=class.forname ("Pro.shaowei.reflect.Person"); Person P=(person) clazz.newinstance (); Method=clazz.getdeclaredmethod ("main", string[]. Class); Method.setaccessible (true); Method.invoke (P, (Object)new string[]{"1", "2"});
Reflection Class-Public fields:
@Test // Reflection Class-Public fields Public void throws exception{ Class clazz=class.forname ("Pro.shaowei.reflect.Person"); Person P=(person) clazz.newinstance (); Field field=clazz.getfield ("Name"); System.out.println (Field.get (P)); " Harry "); P.introduce ();}
Reflection Class-Private fields:
@Test // Reflection Class-Private fields Public void throws exception{ Class clazz=class.forname ("Pro.shaowei.reflect.Person"); Person P=(person) clazz.newinstance (); Field field=clazz.getdeclaredfield ("Age"); Field.setaccessible (true); System.out.println (Field.get (P)); 7); P.introduce ();}
Reflection class private static fields:
@Test // Reflection class private static field Public void throws exception{ Class clazz=class.forname ("Pro.shaowei.reflect.Person"); Person P=(person) clazz.newinstance (); Field field=clazz.getdeclaredfield ("species"); Field.setaccessible (true); System.out.println (Field.get (P)); " Undead "); System.out.println (Field.get (P));}
Java Reflection Learning Notes