Java reflection _ 01, java_01

Source: Internet
Author: User

Java reflection _ 01, java_01

Why reflection?

Example:

Package com. imooc. reflect; public class Work {// define a word method public void word () {System. out. println (" word ");}}
Package com. imooc. reflect; public class Word {public static void main (String [] args) {Work work = new Work (); work. word (); // powerful word work. excel (); // error reported because this method is not available in the work class }}

We hope that this method can be used since the word method is defined, without the influence of the excel () method.

The function of reflection is to avoid the effect of failing to pass during the compilation period. During the runtime, you need to call the word () method, regardless of whether the excel () method exists.

 

The load class at compile time is a static load class, and the load class at run time is a dynamic load class.

Three methods of dynamic loading:

1 package com. imooc. reflect; 2 3 public class ClassDemo1 {4 5 public static void main (String [] args) {6 // how the Instance Object of Foo represents 7 Foo foo1 = new Foo (); 8 // Foo is also an instance object. How does one express the Instance Object of the Class? 9 // any Class is an instance object of the Class, this instance object has three Representation Methods: 10 11 // The first one ---> actually, it tells us that any Class has an implicit static member variable class12 Class c1 = Foo. class; 13 14 // The expression in second already knows that the object of this Class uses the getClass Method 15 class c2 = foo1.getClass (); 16 17/* 18 * c1, c2 indicates the class type of the Foo class 19 * Everything is an object, 20 * the class is also an object, is the Class instance object 21 * This object is called this Class type 22*23 */24 25 // No matter c1 or c2 represents the Class type of the Foo Class, A Class can only be an Instance Object of the Class 26 System. out. println (c1 = c2); 27 28 // third expression 29 Class c3 = null; 30 try {31 c3 = Class. forName ("com. imooc. reflect. foo "); 32} catch (ClassNotFoundException e) {33 // TODO Auto-generated catch block34 e. printStackTrace (); 35} 36 System. out. println (c3 = c2 ); 37 38 // we can create an object instance of the class through the class type ----> Create the Foo Instance Object 39 Foo through c1 or c2 or c3; 40 try {41 foo = (Foo) c1.newInstance (); // constructor with or without parameters 42 foo. print (); 43} catch (InstantiationException e) {44 // TODO Auto-generated catch block45 e. printStackTrace (); 46} catch (IllegalAccessException e) {47 // TODO Auto-generated catch block48 e. printStackTrace (); 49} 50} 51} 52 53 class Foo {54 void print () {55 System. out. println ("hahahahahaha"); 56} 57}

 

The above is a little understanding of java reflection on MOOC. It is certainly not accurate, just to quickly understand reflection. After all, the easier it is, the easier it is to get started. Wait for yourself to reach a certain level and then pursue accuracy. If you want to read this article, do not take this article too seriously.

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.