The reflection mechanism is in the running state, for any Class, you can know all the methods and attributes of this class; for any object, you can call any of its methods and attributes. This
1. Decompilation: .class–>java
Class objects
- public final native Class<?> getClass();
-
package com.chb.reflectTest;public class Test { public static void main(String[] args) throws Exception { //第一种方式: Class<?> c1 = Class.forName("com.chb.reflectTest.Test"); //第二种方式:java中每个类都有class属性 Class<?> c2 = Test.class; //第三种方式:每个对象都与getClass()方法 Class<?> c3 = new Test().getClass(); } }
object, use it to create an object, call the no-argument constructor through newInstance() to create the object, newInstance() returns An Object: Class<?> c1 = Class.forName("com.chb.reflectTest.Test");Object o1 = c1.newInstance();
3.4.1. Obtaining all attributes
Class<?> cString = Class.forName("java.lang.String"); //获取累的修饰和名称 System.out.print(Modifier.toString(cString.getModifiers())+" class " + cString.getSimpleName()+"{\n"); //获取所有属性 Field[] fields = cString.getDeclaredFields(); for (Field field : fields) { System.out.print("\t"); System.out.print( Modifier.toString(field.getModifiers())+" "//属性的修饰 + field.getType().getSimpleName()+" " + field.getName()+"\n"); } System.out.println("}");
Note: I defined two attributes in the Use class, one public, A private, for the next test reflection can break the encapsulationpackage com.chb.reflectTest;public class User {
private String name;
public String nickName;
public User() {}
public User(String name, String nickName) {
this.name = name;
this.nickName = nickName;
}
setter getter...
}
package com.chb.reflectTest;import java.lang.reflect.Field;public class Test1 { public static void main(String[] args) throws Exception { //传统获取属性的值 //1、通过getter,setter User user1 = new User(); user1.setName("lisi"); System.out.println(user1.getName()); //2、直接调用属性 User user2 = new User(); user2.nickName = "癞皮狗"; System.out.println(user2.nickName); //========================================= //通过反射来设置,获取属性。 Class<User> c1 = (Class<User>) Class.forName("com.chb.reflectTest.User"); User user = c1.newInstance(); Field nickField = c1.getDeclaredField("nickName"); nickField.set(user, "123"); System.out.println(nickField.get(user)); Field nameFiled = c1.getDeclaredField("name"); //Exception in thread "main" java.lang.IllegalAccessException: Class com.chb.reflectTest.Test1 can not access a member of class com.chb.reflectTest.User with modifiers "private" nameFiled.setAccessible(true); nameFiled.set(user, "oup"); System.out.println(nameFiled.get(user)); } }
Exception in thread "main" java.lang.IllegalAccessException: Class com.chb.reflectTest.Test1 can not access a member of class com.chb.reflectTest. User with modifiers "private"
nameFiled.setAccessible(true);
1. What is reflection?
. For any class, you can know all the methods and attributes of this class; for any object , you can call any of its methods and attributes. This
##java.lang.reflect.Modifier;
3.2 Three ways to obtain large Class objects
objects
- public final native Class<?> getClass();
-
package com.chb.reflectTest;public class Test { public static void main(String[] args) throws Exception { //第一种方式: Class<?> c1 = Class.forName("com.chb.reflectTest.Test"); //第二种方式:java中每个类都有class属性 Class<?> c2 = Test.class; //第三种方式:每个对象都与getClass()方法 Class<?> c3 = new Test().getClass(); } }
3.3 Create an object
Class<?> c1 = Class.forName("com.chb.reflectTest.Test");Object o1 = c1.newInstance();
3.4. Obtaining data It is divided into all attributes and specified attributes
Then through java.long. reflect.Modifier's toString() prints classes and attribute modifications (public, static, final, etc.).
通过属性对象(Field对象)的getType()
Class<?> cString = Class.forName("java.lang.String"); //获取累的修饰和名称 System.out.print(Modifier.toString(cString.getModifiers())+" class " + cString.getSimpleName()+"{\n"); //获取所有属性 Field[] fields = cString.getDeclaredFields(); for (Field field : fields) { System.out.print("\t"); System.out.print( Modifier.toString(field.getModifiers())+" "//属性的修饰 + field.getType().getSimpleName()+" " + field.getName()+"\n"); } System.out.println("}");
创建测试对象:
注意: 我在Use类中定义了两个属性, 一个public ,一个private ,是为了下一个测试反射可以打破封装性
package com.chb.reflectTest;public class User { private String name; public String nickName; public User() {} public User(String name, String nickName) { this.name = name; this.nickName = nickName; } setter getter... }
package com.chb.reflectTest;import java.lang.reflect.Field;public class Test1 { public static void main(String[] args) throws Exception { //传统获取属性的值 //1、通过getter,setter User user1 = new User(); user1.setName("lisi"); System.out.println(user1.getName()); //2、直接调用属性 User user2 = new User(); user2.nickName = "癞皮狗"; System.out.println(user2.nickName); //========================================= //通过反射来设置,获取属性。 Class<User> c1 = (Class<User>) Class.forName("com.chb.reflectTest.User"); User user = c1.newInstance(); Field nickField = c1.getDeclaredField("nickName"); nickField.set(user, "123"); System.out.println(nickField.get(user)); Field nameFiled = c1.getDeclaredField("name"); //Exception in thread "main" java.lang.IllegalAccessException: Class com.chb.reflectTest.Test1 can not access a member of class com.chb.reflectTest.User with modifiers "private" nameFiled.setAccessible(true); nameFiled.set(user, "oup"); System.out.println(nameFiled.get(user)); } }
通过反射来设置属性的值, 区别属性的修饰范围, 私有的不可以直接设置
会出现访问错误, 也就是java的安全访问机制,报错:
Exception in thread "main" java.lang.IllegalAccessException: Class com.chb.reflectTest.Test1 can not access a member of class com.chb.reflectTest. User with modifiers "private"
我们通过一个方法·来打破java的封装性:
nameFiled.setAccessible(true);
以上就是java之反射基础的内容,更多相关内容请关注PHP中文网(www.php.cn)!