一、Java反射机制的概念:
在程序运行时,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取信息以及动态调用对象的方法的功能称为java语言的反射机制。
二、功能:
在程序运行时判断任意一个对象所属的类;在运行时构造任意一个类的对象;在运行时判断任意一个类所具有的成员变量和方法;在运行时调用任意一个对象的方法;生成动态代理。
三、API使用示例
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- import java.lang.reflect.Modifier;
-
- public class ReflectDemo {
-
- public static void main(String[] args) {
- try {
-
-
- Class class1 = int.class;
- class1 = Cat.class;
-
- class1 = Integer.class;
- class1 = Integer.TYPE;
-
-
-
- class1 = "abc".getClass();
- class1 = new String().getClass();
- class1 = new Cat().getClass();
-
-
- class1 = Class.forName("java.lang.String");
-
-
- Class<Boolean> booleanClass = boolean.class;
- Class<Boolean> booleanClass2 = Boolean.TYPE;
-
- System.out.println("boolean.class==Boolean.TYPE: " + (booleanClass == booleanClass2));
- Class<Byte> byteClass = byte.class;
- Class<Byte> byteClass2 = Byte.TYPE;
- System.out.println("byte.class==Byte.TYPE: " + (byteClass == byteClass2));
- Class<Character> charClass = char.class;
- Class<Character> charClass2 = Character.TYPE;
- System.out.println("char.class==Character.TYPE: " + (charClass == charClass2));
- Class<Short> shortClass = short.class;
- Class<Short> shortClass2 = Short.TYPE;
- System.out.println("short.class==Short.TYPE: " + (shortClass == shortClass2));
- Class<Integer> integerClass = Integer.TYPE;
- Class<Integer> intClass = int.class;
- System.out.println("int.class==Integer.TYPE: " + (intClass == integerClass));
- Class<Long> longClass = long.class;
- Class<Long> longClass2 = Long.TYPE;
- System.out.println("long.class==Long.TYPE: " + (longClass == longClass2));
- Class<Float> floatClass = float.class;
- Class<Float> floatClass2 = Float.TYPE;
- System.out.println("float.class==Float.TYPE: " + (floatClass == floatClass2));
- Class<Double> doubleClass = double.class;
- Class<Double> doubleClass2 = Double.TYPE;
- System.out.println("double.class==Double.TYPE: " + (doubleClass == doubleClass2));
- Class<Void> voidClass = void.class;
- Class<Void> voidClass2 = Void.TYPE;
- System.out.println("void.class==Void.TYPE: " + (voidClass == voidClass2));
- System.out.println("int[][].class == int[][].class: " + (int[][].class == int[][].class));
-
- System.out.println("int.class.isPrimitive(): " + int.class.isPrimitive());
- System.out.println("Integer.class.isPrimitive(): " + Integer.class.isPrimitive());
- System.out.println("int[].class.isPrimitive(): " + int[].class.isPrimitive());
- System.out.println("int[].class.isArray(): " + int[].class.isArray());
-
- System.out.println("---------------------------------------");
- Class<Cat> classz = Cat.class;
-
- Constructor<Cat> constructor = classz.getConstructor(null);
- Cat cat = constructor.newInstance(null);
-
-
- constructor = classz.getConstructor(String.class);
- cat = constructor.newInstance("汤姆猫");
- System.out.println(cat.getName());
-
-
- constructor = classz.getDeclaredConstructor(String.class,String.class);
- constructor.setAccessible(true);
- cat = constructor.newInstance("不吃老鼠的猫","黄猫");
-
-
- Field fieldName = classz.getField("name");
- System.out.println(fieldName.get(cat));
- fieldName.set(cat, "美猫猫");
- System.out.println(fieldName.get(cat));
-
- fieldName = classz.getDeclaredField("type");
- fieldName.setAccessible(true);
- fieldName.set(cat,"挪威森林猫");
- System.out.println(fieldName.get(cat));
-
-
- Method method = classz.getMethod("catchMouse", null);
- method.invoke(cat, null);
-
- method = classz.getMethod("eat", String.class);
- method.invoke(cat, "鱼");
-
- method = classz.getDeclaredMethod("display", null);
- method.setAccessible(true);
- method.invoke(cat, null);
-
-
- Class arraysClass = Class.forName("java.util.Arrays");
- Method arrayMethod = arraysClass.getMethod("sort", int[].class);
- int[] nums = {3,5,9,2,6,10};
- arrayMethod.invoke(null, nums);
- arrayMethod = arraysClass.getMethod("toString", int[].class);
- System.out.println(arrayMethod.invoke(null, nums));
-
-
- System.out.println(classz.getSuperclass().getName());
- Class superClass = classz.getSuperclass();
- Method[] methods = superClass.getDeclaredMethods();
- for (Method method2 : methods) {
- System.out.print(method2.getName() + " ");
- }
- System.out.println();
-
- int modifier = superClass.getModifiers();
- System.out.println(Modifier.isAbstract(modifier));
-
-
- Field[] fields = superClass.getDeclaredFields();
- for (Field field : fields) {
- System.out.print(field + " ");
- }
- System.out.println();
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-
- abstract class Animal {
-
-
- public String name;
-
-
- private String color;
-
-
- private int age;
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getColor() {
- return color;
- }
-
- public void setColor(String color) {
- this.color = color;
- }
-
-
- public abstract void eat(String name);
-
- }
-
- class Cat extends Animal {
-
-
- private String type;
-
- public Cat() {
- System.out.println("Cat");
- }
-
- private Cat(String name,String type) {
- this.name = name;
- this.type = type;
- }
-
- public Cat(String name) {
- this.name = name;
- }
-
- public String getType() {
- return type;
- }
-
- public void setType(String type) {
- this.type = type;
- }
-
-
- public void catchMouse() {
- System.out.println("抓老鼠。。。");
- }
-
- @Override
- public void eat(String foodName) {
- System.out.println("猫吃" + foodName + "。");
- }
-
-
- private void display() {
- System.out.println("name:" + name);
- }
-
- }