反射机制脑补

一、概念



简而言之,反射机制就是不需要要使用强引用的方式也就是new 的方式来获得一个类的引用

java在执行程序之前都必须经过编译、加载、连接和初始化

(1)加载:查找并加载二进制的数据

(2)连接:验证正确性,分配内存,解析数据

(3)初始化:为静态变量进行赋值


 

二、程序解析



直接通过程序来验证一下方式机制的具体使用

ackage ThreeWeek;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

class People{
	private String name;
	private int age;
	
	public void eat(){
		
	}
	
	public void  speak(String word){
		System.out.println(word);
	}
}

class Student extends People{
	private int studentID;
	private int score;
	
	public void People(int studentID, int score){
		this.score = score;
		this.studentID = studentID;
	}
	
	public void learn(){
		System.out.println("学生正在学习");
	}
	

	public int getStudentID() {
		return studentID;
	}

	public void setStudentID(int studentID) {
		this.studentID = studentID;
	}

	public int getSocre() {
		return score;
	}

	public void setSocre(int socre) {
		this.score = socre;
	}
}

public class ReflectionTest {

	public static void main(String args[]) throws InstantiationException, 
												  IllegalAccessException, 
												  IllegalArgumentException, 
												  InvocationTargetException, 
												  NoSuchMethodException, 
												  SecurityException, 
												  NoSuchFieldException{
		
		//获得class第一种方式
		System.out.println("第一种方式");
		Student s = new Student();
		System.out.println(s.getClass().getName());
		System.out.println(s.getClass().getSimpleName());
		
		//获得class第二种方式
		System.out.println("第二种方式");
		Class<Student> stu = Student.class;
		System.out.println(stu.getName());
		System.out.println(stu.getSimpleName());
		
		//获得class第三种方式
		System.out.println("第三种方式");
		try {
			Class<Student> stuu = (Class<Student>)Class.forName("ThreeWeek.Student");
			System.out.println(stuu.getName());
			System.out.println(stuu.getSimpleName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		
		//获得父类的名称
		System.out.println("第一种方式");
		System.out.println(s.getClass().getSuperclass());
		System.out.println(stu.getSuperclass().getSimpleName());
		
		//获得所有的公开方法
		System.out.println("获得所有公开的方法");
		Method[] methods = stu.getMethods();
		for(Method method : methods){
			System.out.println(method.getName());
		}
		
		//获得本类中定义的方法
		System.out.println("获得本类中定义的方法");
		Method[] declaredMethods = stu.getDeclaredMethods();
		for(Method method : declaredMethods){
			System.out.println(method.getName());
		}
		
		//获得本类中定义的属性
		System.out.println("获得本类中定义的属性");
		Field[] declaredFields = stu.getDeclaredFields();
		for(Field field : declaredFields){
			System.out.println(field.getName());
		}
		
		//获得公开的属性
		System.out.println("获得本类中公开的属性");
		Field[] fields = stu.getFields();
		for(Field field : fields){
			System.out.println(field.getName());
		}
		
		//调用本类中定义的方法
		System.out.println("调用方法");
		Method methodTest = stu.getMethod("learn");
		methodTest.invoke(stu.newInstance());
		
		//创建类的对象
		System.out.println("创建类的对象");
		Student instance = stu.newInstance();
		instance.setSocre(99);
		instance.setStudentID(123);
		System.out.println("学生成绩"+instance.getSocre()+"学生ID"+instance.getStudentID());
		
		//得到类的构造函数
		System.out.println("得到类构造函数");
		Constructor<Student> constructor = stu.getDeclaredConstructor();
		Student newInstance = constructor.newInstance();
		newInstance.setSocre(111);newInstance.setStudentID(8888);
		System.out.println(newInstance.getSocre()+" "+newInstance.getStudentID());
		
		//反射机制操作成员变量
		Student obj = stu.newInstance();
		Field declaredField = stu.getDeclaredField("score");
		declaredField.setAccessible(true);
		declaredField.set(obj, 9999);
		System.out.println("修改后的属性的值是"+declaredField.get(obj));
	}
}


---------------------------------------output--------------------------------------------

第一种方式
ThreeWeek.Student
Student
第二种方式
ThreeWeek.Student
Student
第三种方式
ThreeWeek.Student
Student
第一种方式
class ThreeWeek.People
People
获得所有公开的方法
setStudentID
getStudentID
learn
setSocre
getSocre
People
eat
speak
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAll
获得本类中定义的方法
setStudentID
getStudentID
learn
setSocre
getSocre
People
获得本类中定义的属性
studentID
score
获得本类中公开的属性
调用方法
学生正在学习
创建类的对象
学生成绩99学生ID123
得到类构造函数
111 8888
修改后的属性的值是9999


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值