JAVA Study Notes ()-Reflection reflection, Study Notes Reflection

Source: Internet
Author: User

JAVA Study Notes ()-Reflection reflection, Study Notes Reflection
Reflection

Import java. lang. reflect. modifier;/** Reflection * java. lang. class class */public Class Test01 {public static void main (String [] args) {String name = "tom"; // Method 1: getClass () method // any Class object has a getClass () method // you can use this method to obtain the Class Object cls = name corresponding to the current object type. getClass (); showInfo (cls); // Method 2: Use the forName () Static Method of the Class try {Class cls2 = Class. forName ("com. itany. basejava. day37.Test01 "); showInfo (cls2);} catch (ClassNotFoundException e) {e. printStackTrace ();} // method 3: Specify the class attribute of the Class cls3 = Student. class; showInfo (cls3);} // obtain information about the Class Object public static void showInfo (Class cls) {System. out. println ("full name of the class:" + cls. getName (); System. out. println ("Class Name:" + cls. getSimpleName (); System. out. println ("package:" + cls. getPackage (); System. out. println ("whether it is a local class:" + cls. isLocalClass (); System. out. println ("whether it is an interface:" + cls. isInterface (); System. out. println ("array:" + cls. isArray (); System. out. println ("is the basic data type:" + cls. isPrimitive (); // obtain the Class Object Class superCls = cls of its parent Class. getSuperclass (); System. out. println ("full name of the parent class:" + superCls. getName (); // get the class modifier int m = cls. getModifiers (); System. out. println ("Modifier:"); if (Modifier. isPublic (m) {System. out. print ("public");} if (Modifier. isStatic (m) {System. out. print ("static");} if (Modifier. isAbstract (m) {System. out. print ("abstract");} if (Modifier. isFinal (m) {System. out. print ("final");} System. out. println ("************************************* * \ n ");}}
Instantiate an object through the reflection access Constructor
Import java. lang. reflect. constructor; import java. lang. reflect. method;/** use reflection to access the Constructor class */public class Test03 {public static void main (String [] args) throws Exception {// create a Dog Class Object // Dog dog1 = new Dog ("rhubarb", 3, "male "); // create a Class object Class <Dog> cls = Dog for the Dog Class. class;/** instantiate an object without a constructor. Two methods are available * // 1. the newInstance () method of the Class object is used to create an instance. It is only applicable to the creation of the object Dog = (dog) cls through the no-argument constructor. newInstance (); // dog. show (); Method method = cls. getDeclaredMethod ("show"); method. invoke (dog); System. out. println ("************************** \ n"); // 2. constructor c = cls. getDeclaredConstructor (); Dog dog2 = (Dog) c. newInstance (); dog2.show (); System. out. println ("************************** \ n "); /** instantiate the object with the Constructor c2 = cls. getDeclaredConstructor (new Class [] {String. class, int. class, String. class}); Dog dog3 = (Dog) c2.newInstance ("yellow", 2, "female"); dog3.show (); /** instantiate the object through the private modifier Construction Method */Constructor <Dog> c3 = cls. getDeclaredConstructor (new Class [] {String. class, String. class}); c3.setAccessible (true); Dog dog4 = c3.newInstance ("", "中""); dog4.show ();}} /** Animal parent class */class Animal {public String name; int age; public Animal () {System. out. println ("constructor without parameters of the parent class");} public Animal (String name, int age) {this. name = name; this. age = age; System. out. println ("constructor with parameters in the parent class");} public void show () {System. out. println ("show method in parent class") ;}/ ** Dog class, inherited from Animal */class Dog extends Animal {String sex; public Dog () {System. out. println ("constructor without parameters in the subclass");} public Dog (String name, int age, String sex) {System. out. println ("constructor with parameters in subclass");} private Dog (String name, String sex) {System. out. println ("constructor with two parameters in the subclass");} // rewrite the show method public void show () {System in the parent class. out. println ("show method in subclass ");}}
Field Class
Import java. lang. reflect. field; import java. lang. reflect. invocationTargetException; import java. lang. reflect. method;/** Field class */public class Test02 {public static void main (String [] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, invocationTargetException {// create a Student class object, that is, Student stu1 = new Student (); Student stu2 = new Student (); // create a Student class object, that is, the Class Object Class cls = Student of the Student Class. class;/** Field attribute operation; * // 1. obtain all attributes in the Student class, including all attributes modified by the public in the class and the parent class. // Field [] fields = cls. getFields (); // 2. obtain all attributes in the Student class, including all attributes modified by any modifier in the class Field [] fields = cls. getDeclaredFields (); for (Field f: fields) {System. out. println (f. getName (); // System. out. println (f. toString ();} System. out. println ("*********************** \ n"); // 3. obtains the specified Field name = cls in the Student class. getField ("name"); Field grade = cls. getDeclaredField ("grade"); Field sex = cls. getSuperclass (). getDeclaredField ("sex"); // assign a value to the attribute // stu1.name = "tom"; // directly use the object name. attribute name assignment name. set (stu1, "alice"); // assign System to the attribute of the specified object. out. println ("stu1 name:" + name. get (stu1); // set the attribute to accessible, that is, set the access modifier grade. setAccessible (true); grade. set (stu1, 2); // assign System to the private attribute grade of stu1. out. println ("stu1 grade:" + grade. get (stu1); System. out. println ("************************************* * *** \ n "); /** Method operation * // 1. obtain all methods in the Student class // Method [] methods = cls. getMethods (); Method [] methods = cls. getDeclaredMethods (); for (Method m: methods) {System. out. println (m. getName ();} // 2. obtain the specified method in the Student Class // stu1.calc (10, 25); Class [] args1 = {int. class, double. class}; Method calc = cls. getMethod ("calc", args1); Object [] args2 = {10, 25.4}; double sum = (Double) calc. invoke (stu1, args2); System. out. println ("sum of two numbers:" + sum); // Method for obtaining private modification show = cls. getDeclaredMethod ("show"); show. setAccessible (true); // The access modifier show is not detected. invoke (stu2); // call method}/** Person class */class Person {public String name; int age; private String sex; protected double height ;} /** Student class, inherited from Person */class Student extends Person {public int id; // Student id private int grade; // grade number String className; // class name public Student () {} public int getGrade () {return grade;} public void setGrade (int grade) {this. grade = grade;} public int calc (int num1, int num2) {return num1 + num2;} public double calc (int num1, double num2) {return num1 + num2 ;} private void show () {System. out. println ("the sixth row of the second row is a bit 22222 ");}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.