/** * @authorAdministrator * Benefits: Generics: 1 Security 2 reduced code reuse rate*/ Packagecom.test;ImportJava.lang.reflect.Method; Public classTest2 { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//gen<string> gen1 = new Gen<string> ("AAA");//gen1.showtypename ();// //gen<integer> Gen2 = new gen<integer> (1);//gen2.showtypename ();Gen<Bird> Gen3 =NewGen<bird> (NewBird ()); Gen3.showtypename (); //the reflection mechanism of Java generics }}//Define a birdclassbird{ Public voidtest1 () {System.out.println ("AA"); } Public voidCountintAintb) {System.out.println (a+b); }}//define a classclassGen<t>{ PrivateT o; //constructor Function PublicGen (T a) { This. O =A; } //get the type and name of T Public voidShowtypename () {System.out.println ("Type is:" +O.getclass (). GetName ()); //through the reflection mechanism can get the information of the class, name, return value, modifier, etc.//through the reflection mechanism, we can get a lot of information about this type of T//get the name of the member functionMethod[] m =O.getclass (). Getdeclaredmethods (); //Print for(inti = 0; i < m.length; i++) {System.out.println (M[i].getname ()); } }}
Java Generic reflection mechanism (ii)