Package 01: package Reflectionconstructor; public class Reflectionfieldpoint { private int X; public int Y; public reflectionfieldpoint (int x, int y) { super (); this . x = X; this . Y = Y; }}Package 02:PackageReflectionconstructor;ImportJava.lang.reflect.Field;/*** Reflection, * get member variable*/ Public classReflectionfield { Public Static voidMain (string[] args)throwsException {reflectionfieldpoint point1
The reflection is described in the previous article, the following is mainly about the actual use of some reflection.By reflecting us, I can get the interface and also get the class that implements the interface, at which point the interface reference can access an instance of the implementation class.I first defined an interface: Public Interface IPerson { void SetName (string name); void SayHello (); }To define a cla
Reflection is primarily used to dynamically parse the class name, namespace, properties, methods, and actions of related classes during a program run , as described in the following two simple examples:Example 1: Invoking an internal method of an assembly that dynamically obtains information about the class, including the class name, namespace, and the creation of the object and the invocation of the method:Test class:classhi{ Public stringHi ="HelloW
The code is very simple and I will not talk about it much. Paste code and data directly.
1: Compiler call
Code
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->UserCore core = new UserCore ();
Core. GetCount (1, 50000279 );
2: Reflection
Code
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->Assembly classSampleAssembly = Assembly. LoadFrom (@ "C: \ SN
Java core-Reflection (1), java core-ReflectionJVM and class:Run java: java class with the main method to start jvm, and load the bytecode --------------------------------------------- when you call the java command to run a java program, the command will start a jvm process, all the threads and variables in the same jvm are in the same process. When the memory area of the jvm is shared, the JVM exits when the following conditions occur: (1) normal exe
Concept
In Java, reflection maps various components in Java classes into corresponding Java classes.
Case
Reflection can be used to replace strings. For example, two string members are defined in the mystring class as follows:
PublicString str1 = "you did it well ";PublicString str2 = "good ";
If you want to use the reflection principle to replace so
][3]:Class ClassType = Class.forName ("java.lang.String");Int[] dimension = new int[]{10,10,10};Object array2_3d = array.newinstance (ClassType, dimension);Gets the first array of three-dimensional array component Array2obj, which is a two-dimensional arrayObject array2obj_2d = Array.get (array2_3d, 1);Gets the second array component of Array2obj_2, which is a one-dimensional arrayObject array2obj_1d = Array.get (array2obj_2d, 2);Sets the value of the third element of a array2obj_1Array.set (arr
Dynamic | view | combination Query
Reflection is one of the characteristics of Java program development language, it allows the running program to access itself, and can directly manipulate the internal properties of the program.
Hibernate is a Java environment-oriented object/relational database mapping tool, basically, each table or view in the hibernate can correspond to a class, where we make full use of this class, to implement dynamic combina
Java reflection mechanism, java reflection
Public static void main (String [] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {Running in = new Processing (System. in );String className = in. nextLine ();// Dynamic loading classClass // Dynamically create a Class ObjectObject in
// The ClsPublic class in the dynamic link library has a variable private static string key = "1111 ";// Modify and obtain the value through the reflection technique below// Set the keyPublic static void updatePalmKey (string key = "test") {BindingFlags flag = BindingFlags. static | BindingFlags. nonPublic; FieldInfo f_key = typeof (ClsPublic ). getField ("key", flag); f_key.SetValue (new ClsPublic (), key );}////// Obtain the key //////
Public stati
return value -M2.invoke (obj, "hello");////call method of the person class + - // Gets the Class A member method that has a single parameter that has a public type of return value +Method m3 = C.getmethod ("getString", String.class,int.class); A // There is a return value, public object Invoke (Object Obj,object ... args) The return value is Object receive atObject ob1 = M3.invoke (obj, "Hello", 123); - System.out.println (OB1); - - // Gets a privat
1. How to get a methodThe name of the method and the parameter list of the method can only determine a method2. Actions reflected by the methodMethod.invoke (); PackageCom.tsh.reflect;ImportJava.lang.reflect.Method; Public classReflectdemo { Public Static voidMain (string[] args) {p P=NewP (); Class C=p.class; Try{Method Method=c.getdeclaredmethod ("Print", String.class); Method.invoke (P,"SSSs"); } Catch(Exception e) {e.printstacktrace (); } }}classp{ Public voidPrintintAintb) {Syste
Modifier -System.out.println (R.getname () + "");//Output Attribute type -System.out.println (F[i].getname () + ";");//Output Attribute name + } -System.out.println ("----------Output a public property is an implemented interface or a public property in a parent class----"); +Field[] f = c.getfields ();//Get Properties A at for(inti=0; I ){ -class//gets the type of the property - intMo = F[i].getmodifiers ();//Get modifier number -String priv = modifier.tostring (M
Java reflection comprehension and Java reflection ComprehensionReflection: Get the class when the object is running.Person. class --> Person object Class. class --> Class ObjectWe create a Person object through the Person. class bytecode file to represent a real Person. The Person object is used to map a Person. It is not a bytecode file on the hard disk, but a Person object created based on the loading cla
Examples of common Java reflection methods and java reflection examples
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
Java provides a set of mechanisms to dynamically execute methods, constructor methods, and array operations. This mechanism is called reflection. Reflection mechanisms are the basis for implementation of many popular frameworks today, including spring and hibernate. The principle issue is not the focus of this Article. Next, let's learn about this wonderful mechanism in the instance.
Public object getproper
The following code is a tool class
Package com.imooc.reflect;
Import Java.lang.reflect.Method; public class Classutil {public static void Printclassmessage (Object obj) {//To get information about the class, first get the class type class C = Obj.getcl
Ass ();//Pass the object of which subclass, C is the class type of the subclass//Get the class name System.out.println ("Class name is:" +c.getname ()); * * Methods class, Method Object * A member method is a Methods object * GetMethods
PHP5 added a new feature: Reflection. This feature allows programmers to
reverse-engineer[Reverse engineering] class, Interface,function,method and extension[Extension Library support].
With PHP code, you get all the information about an object and you can interact with it.
For example, assume the following person class:
Class Person { /** * For the sake of demonstration, we ' re setting this private */ private $_allowdynamicat Tributes
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.