The real use of reflection-thoughts on NetEase Cloud Classroom Intelligence Podcast Fanglixun Teacher reflection video small Mistakes

Source: Internet
Author: User

I like to watch videos of the more famous Java lecturers because I don't have a professional Java study. Recently was watching Fanglixun teacher's video, but in the reflection of the time (class 12~16), found a bit of a problem.

The teacher said in reflection: "If the specific method to invoke the class and class is passed by the user, and the programmer does not know what will be passed in, then this time to use reflection." Load the class through reflection, and then execute the method. For example, JDBC has a database-driven class that uses reflection.

But in the teacher's video, the example he was talking about actually used the new construction method, and here is an example he wrote:

Class clazz=class.forname ("Com.learn.reflect.Person"); The method names---This class name and the next line are the user's incoming
Method M=clazz.getmethod ("Print3", String[].class);
Person P=new person ();
M.invoke (P, (Object) New string[]{"1", "23"});

At that time, I thought it was a good story. But today I saw the second time that the situation was not right: personp=new person (); What is this code in the red area? It's not a good idea. Do you not know which object to pass in? What's the new object? Then consulted the developer sitting next to my colleague, finally figured out that the reflection is not so used, the above code can not be used to write the reflection code. The real reflection usage is this:

Class Clazz=class.forname ("Com.learn.reflect.Person");--first or load the target class

Object obj=clazz.newinstence;

Method M=clazz.getmethod ("Print3", String[].class); --string[].class and "Print3" are all used by users.

M.invoke (obj, (Object) New string[]{"1", "23"});

Note: The above is about the target method of running the target class through reflection. The method of obtaining the target property of the target class by reflection because it does not need to use the object, so you can see the teacher's video.

Another: I do not mean that the black teacher, because: 1. I do not feel that the teacher must clearly each aspect of the 2. I also can not black Fang teacher. I am grateful to the teacher of the ministry, so call him as a teacher, influenced by him, I also share this discovery. 12~16 these 5 hours of defects do not cover , Fang teacher said very detailed, on whether the method has a return value, is not public, the type and number of parameters, the teacher has explained in detail how to call through reflection.

The real use of reflection-thoughts on NetEase Cloud Classroom Intelligence Podcast Fanglixun Teacher reflection video small Mistakes

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.