zoukankan      html  css  js  c++  java
  • Java—反射

    一、反射原理

    通过一个路径,Java反射机制(类的加载器)自动创建一个对象

    JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。


    二、获取类的三种方式

    Student类

    public class Student {
    	private String name;
    
    	private Student() {
    	}
    	
    	public Student(int age) {
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	public void test(int i){
    		System.out.println("test方法" + i);
    	}
    	
    }
    
    public class ReflectDemo {
    	public static void main(String[] args) throws Exception {
    		// 获取Class对象的三种方式:
    		// 方式一
    //		Class clazz = Student.class;
    		
    		// 方式二:常用
    //		Class<?> clazz = Class.forName("cn.kooun.reflect.Student");
    		
    		// 方式三
    		Student s = new Student();
    		Class<? extends Student> clazz = s.getClass();
    		System.out.println(clazz);
    	}
    }
    

    三、通过反射获取构造器

    public class ReflectDemo {
    	public static void main(String[] args) throws Exception {
    		Class<?> clazz = Class.forName("cn.kooun.reflect.Student");
    		
    		// 通过反射获取构造器
    //		Constructor<?> constructor = clazz.getConstructor(int.class);
    //		System.out.println(constructor);
    		
    //		Constructor<?>[] constructors = clazz.getConstructors();
    //		for (Constructor<?> c : constructors) {
    //			System.out.println(c);
    //		}
    		
    		// 暴力获取构造器(可以获取私有的)
    		Constructor<?> declaredConstructor = clazz.getDeclaredConstructor();
    		// 取消 Java 语言访问检查,绕过修饰权限
    		declaredConstructor.setAccessible(true);
    		System.out.println(declaredConstructor);
    	}
    }
    

    四、通过反射创建对象和调用方法

    public class ReflectDemo {
    	public static void main(String[] args) throws Exception {
    		Class<?> clazz = Class.forName("cn.kooun.reflect.Student");
    		
    		// 暴力获取构造器(可以获取私有的)
    		Constructor<?> declaredConstructor = clazz.getDeclaredConstructor();
    		// 取消 Java 语言访问检查,绕过修饰权限
    		declaredConstructor.setAccessible(true);
    		System.out.println(declaredConstructor);
    
    		// 创建实例
    		Object newInstance = declaredConstructor.newInstance();
    		// 通过反射获取方法
    		Method method = clazz.getMethod("test", int.class);
    		method.invoke(newInstance, 10);
    	}
    }
    

    五、通过反射获取成员变量

    public class ReflectDemo01 {
    	public static void main(String[] args) throws Exception {
    		Class<?> clazz = Class.forName("cn.kooun.reflect.Student");
    
    		// 暴力获取构造器(可以获取私有的)
    		Constructor<?> declaredConstructor = clazz.getDeclaredConstructor();
    		// 取消 Java 语言访问检查,绕过修饰权限
    		declaredConstructor.setAccessible(true);
    		System.out.println(declaredConstructor);
    		
    		Object newInstance = declaredConstructor.newInstance();
    		
    		// 通过反射获取成员变量
    		Field field = clazz.getDeclaredField("name");
    		field.setAccessible(true);
    		field.set(newInstance, "Sunny");
    		System.out.println(field.get(newInstance));
    	}
    }
    
  • 相关阅读:
    MarkDown语法总结
    HashMap
    [LeetCode] 102. Binary Tree Level Order Traversal(二叉树的中序遍历)
    [LeetCode] 287. Find the Duplicate Number(寻找重复数字)
    [LeetCode] 215. Kth Largest Element in an Array(数组里的第 k 大元素)
    [LeetCode] 39. Combination Sum(组合的和)
    [LeetCode] 49. Group Anagrams(分组相同字母异序词)
    [LeetCode] 48. Rotate Image(旋转图片)
    [LeetCode] 647. Palindromic Substrings(回文子串)
    [LeetCode] 238. Product of Array Except Self(数组除自身元素外的乘积)
  • 原文地址:https://www.cnblogs.com/nadou/p/13985560.html
Copyright © 2011-2022 走看看