***

1、 What is reflection mechanism? In short, reflection mechanism refers to that a program can obtain its own information at run time. In Java, as long as the name of the class is given, all the information of the class can be obtained through the reflection mechanism.

2、 Where to use the reflection mechanism? Sometimes, we have used some knowledge, but we don't know what its technical term is. When we just learned JDBC, we used a line of code, class forName("com.MysqL.jdbc.Driver.class"). newInstance(); But at that time, I only knew that the line of code was to generate the driver object instance, and I didn't know its specific meaning. After listening to the lesson of reflection mechanism, I know that this is reflection. Now many open frameworks use reflection mechanism. Hibernate and struts are implemented by reflection mechanism.

3、 Advantages and disadvantages of reflection mechanism why use reflection mechanism? Don't you just create objects directly? This involves the concepts of dynamic and static. Static compilation: determine the type and bind the object at compile time, that is, through. Dynamic compilation: determine the type and bind objects at runtime. Dynamic compilation maximizes the flexibility of Java, reflects the application of polymorphism, and reduces the coupling between classes. In a word, the advantage of reflection mechanism is that it can dynamically create objects and compile, which reflects great flexibility, especially in the development of J2EE. For example, a large-scale software cannot be designed perfectly at one time. When the program is compiled and released, when it is found that some functions need to be updated, we can't ask users to uninstall the previous software and reinstall the new version. If so, the software must not be used by many people. If it is static, the whole program needs to be recompiled once to realize the function update. If it adopts the reflection mechanism, it can realize the function without unloading. It only needs to be dynamically created and compiled at runtime.

Its disadvantage is that it has an impact on performance. Using reflection is basically an interpretation operation. We can tell the JVM what we want to do and it meets our requirements. Such operations are always slower than just performing the same operation directly.

4、 What information can be obtained by using the reflection mechanism? In a word, it can obtain what information is available in the class, but the premise is to know the name of the class, or there will be no later text. First, create a class object according to the full name of the incoming class. Class c=Class. forName("className"); Note: classname must be the full name, that is, it must include the package name, such as CN netjava. pojo. UserInfo; Object obj=c.newInstance();// Creating an instance of an object is OK. With an object, everything is easy to do. You can have whatever information you want. Obtain the constructor method getconstructor (class [] params) / / obtain the public constructor according to the specified parameters

Constructor [] getconstructors() / / get all public constructors

Constructor getdeclaraedconstructor (class [] params) / / obtain public and non-public constructors according to the specified parameters

Constructor [] getdeclaredconstructors() / / get all public constructors

Obtain the method getmethod (string name, class [] parameters) of the class method, and obtain the method according to the method name and parameter type

Method [] getmethods() / / get all public methods

Method getdeclaraedmethod (string name, class [] params) / / obtain public and non-public methods according to the method name and parameter type

Method [] getdeclaredmethods() / / get all public and non-public methods

Get the method field getfield (string name) of the attribute in the class / / get the corresponding public variable according to the variable name

Field [] getfields() / / get all public methods in the class

Field getdeclaraedfield (string name) / / obtain public and non-public variables according to the method name

Field [] getdeclaredfields() / / get all the public and non-public methods in the class. That's all. If you know these, everything else is easy... 5. What can you do with the reflection mechanism? At the beginning, when you use JDBC, when writing and accessing the database, you write that you feel sick. There are eight tables. Each table has an operation of adding, deleting, modifying and querying. At that time, you didn't know the concept of the reflection mechanism, Therefore, create different Dao classes for different tables. In this way, not only the development speed is fast, but also the code redundancy is strong. The most fatal thing is to look at the same, and then directly copy and modify. Because it is easy to make various low-level errors (case, one more or one less letter...), one error can make you look for half a day.

With the java reflection mechanism, everything is easy to do. You only need to write a Dao class, four methods, add, delete, modify and query, and pass in different objects. You don't need to create Dao classes for each table. The reflection mechanism will automatically help us complete the rest. That's its advantage. To put it bluntly, the reflection mechanism is designed to help us do those repetitive and regular things, so many software that automatically generate code now use the reflection mechanism. As long as you input relevant parameters according to the rules, low-level programmers are slowly erased. Why? Because you don't have to write code. Anyone can develop it. What do you want programmers to do? So we have only one way out, that is to work hard and try again to become a senior programmer, specially develop fool software, and let other programmers cool off, ha ha~

6、 The basic principle of adding and querying database data with reflection mechanism; When saving data, take out all the attribute values of the objects to be saved, and then piece them together. When querying SQL statements, wrap all the queried data into a Java object. Rules of the game: as the saying goes, no rules make no difference. Especially for programs, they can only do things with rules. If there are no rules, they can't do it. Well, set the rules first. 1) each table object in the database has a POJO class, and each field in the table corresponds to an attribute in the POJO class. In addition, the name of the POJO class is the same as that of the table, and the attribute name is the same as that of the field. It doesn't matter whether it is case sensitive, because the database is generally case insensitive. 2) add standard set and get methods for each attribute in the POJO class. With the rules of the game, let's start the game.

1. First of all, there is a table in the database. Suppose the database name is blogsystem, and a table name in it is userinfo. As shown in the figure:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>