Java: class9 reflection, class Object

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43336822/article/details/101549460

A reflection mechanism

  1. Program run time access to information in their own category. As long as the class name will be able to use reflection.
    java program running procedure:
    (. 1) .java ----------- the javac compiler generates .class files (bytecode file) byte code file is run --------- converted to a class object.
    (2) can be acquired reflection information:
    ① Properties (Fields) ② member methods (Methods) ③ constructor (the Constructor)

Two objects .Class

1. In the java world, everything is an object. In a sense, java There are two kinds of objects: Class instance of an object and the object. Runtime type information of each class is represented by Class object. It contains information about the class. In fact, we came to the object instance created by the Class object. Java Class object performs its use RTTI (runtime type identification, Run-Time Type Identification), polymorphism is implemented based RTTI.

2. Get Class object in three ways:
(1) the class name .class: Class Class1 = Student.class;
(2) the Class.forName ( "fully qualified name of the class [package class name.]");: Class.forName ( "TuLun.Student");
(. 3) Object .getClass ();: Class class3 = new Student () getClass ();.

3. By reflecting a new object (interview questions)
Class CLA = Student.class;
the Constructor cla.getConstructor = constructor ();
Student = STU (Student) Constructor.newInstance ();

Guess you like

Origin blog.csdn.net/qq_43336822/article/details/101549460