1. Operation class
Gets the class and gets an instance object through reflection
Class Class1 = Student.class;
Student Student = (Student) class1.newinstance (); Default call no parameter construction method
Student.setname ("heh");
System.out.println (Student.getname ());
2. Operation Construction Method
gets the constructor method for the specified parameter type that creates an instance object of a specific parameter value from this object
Class Class1 = Student.class;
constructor<?> Constructor = Class1.getconstructor (String.class,string.class,int.class);
Student student1= (Student) constructor.newinstance ("123", "456", 12);
System.out.println (Student1.getage ());
3. General method of operation
Class Class1 = Student.class;
Student student= (Student) class1.newinstance ();
Method Method=class1.getmethod ("Setage", Int.class);
Method.invoke (student,10);
System.out.println (Student.getage ());
4. Operation Properties
Class Class1 = Student.class;
Student Student = (Student) class1.newinstance ();
Field Fiel=class1.getdeclaredfield ("Age");
Fiel.setaccessible (TRUE); The member variable in the Accessibletest class is private, so you must do this
Fiel.set (student, 10);
System.out.println (Student.getage ());
5. Modifying the value of an array
Int[] a= {1,2,3,4,5,6,7,8,9};
Class Class1 = A.getclass (). Getcomponenttype (); Returns the Class that represents the array component type. If this class does not represent an array class, this method returns null
System.out.println ("Name of the array data:" +class1.getname ());
System.out.println ("Length of the array:" +array.getlength (a));
System.out.println ("The value of the first element of the data:" +array.get (a,0));
Array.set (A, 0, 12);
System.out.println ("The value of the first element of the data:" +array.get (a,0));
This article is from the "Programmer rookie" blog, be sure to keep this source http://5345468.blog.51cto.com/5335468/1690318
Reflection operations classes, methods, properties, etc.