Java反射

java 复制代码
package com.qcby;

public class Person {
	private String name;
	public Integer age;
	Character sex;
	protected Integer height;
	
	public Person() {
		
	}

	public Person(String name) {
		this.name = name;
	}
	
	public Person(String name, Integer age, Character sex, Integer height) {
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.height = height;
	}
	
	public void run() {
		System.out.println("人跑的很快");
	}
	
	private void fly(String name) {
		System.out.println(name+"很会飞");
	}
	
	int gerAge() {
		return age;
	}
	
	void method(Integer age , Character sex) {
		System.out.println(age + "  " + sex);
	}
}
java 复制代码
package com.qcby.user;


//成员变量赋值,输出值,执行构造器,运行方法
public class User {
	private String name;
	public Integer age;
	Character sex;
	protected Integer height;
	
	public User() {
		
	}

	public User(String name) {
		this.name = name;
	}
	
	private User(String name, Integer age, Character sex, Integer height) {
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.height = height;
	}
	
	public void run() {
		System.out.println("人跑的很快");
	}
	
	private void fly(String name) {
		System.out.println(name+"很会飞");
	}
	
	int getAge() {
		return age;
	}
	
	void method(Integer age , Character sex) {
		System.out.println(age + "  " + sex);
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", sex=" + sex + ", height=" + height + "]";
	}
}
java 复制代码
package com.qcby.user;

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

public class Main {
	public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
		Class pclass = 	Class.forName("com.qcby.user.User");
		Constructor userconstructor  = pclass.getDeclaredConstructor();
		User user = (User) userconstructor.newInstance(null);
		Field namefield =  pclass.getDeclaredField("name");
		namefield.setAccessible(true);
		namefield.set(user, "qiqi");
		Field agefield =  pclass.getDeclaredField("age");
		agefield.set(user, 1);
		Field sexfield =  pclass.getDeclaredField("sex");
		sexfield.set(user, '2');
		Field heightfield =  pclass.getDeclaredField("height");
		heightfield.set(user, 11);
		System.out.println(user.toString());
		Method runmethod  =  pclass.getDeclaredMethod("run", null);
		Method flymethod =  pclass.getDeclaredMethod("fly", String.class);
		flymethod.setAccessible(true);
		Method getAgemethod =  pclass.getDeclaredMethod("getAge", null);
		Method methodmethod =  pclass.getDeclaredMethod("method", Integer.class,Character.class);
		runmethod.invoke(user, null);
		flymethod.invoke(user, "qiqi");
		getAgemethod.invoke(user, null);
		methodmethod.invoke(user, 11,'a');
		
		
	}
}
相关推荐
sharemylove4 小时前
Java集合框架全面详解
java·开发语言·经验分享·笔记·学习
FYKJ_20104 小时前
springboot众创项目团队管理系统94666-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·spark·课程设计
摇滚侠6 小时前
《On Java 中文版 基础卷》阅读笔记 内部类 20
java·笔记·python
郝学胜-神的一滴8 小时前
Effective Python 条款43:自定义容器类
开发语言·windows·python·pandas·scikit-learn·pyqt
FYKJ_20108 小时前
springboot贫困生认定管理系统59009-计算机毕业设计、课程设计
java·javascript·vue.js·spring boot·后端·django·课程设计
考虑考虑9 小时前
JDK27中的结构化并发
java·后端·java ee
好焊孙辉9 小时前
一条产线省下 30 个电机位:Z 轴和 R 轴,该合成一个了
开发语言·r语言
布莱克6059 小时前
Swift 中的逃逸闭包与非逃逸闭包:@escaping 到底在做什么
开发语言·ios·swift·闭包·逃逸
郝学胜-神的一滴10 小时前
Effective STL 条款 14:用扩容函数避免重新分配
linux·开发语言·网络·数据结构·c++·单片机
吃我一窝jio10 小时前
Java IO流核心分类与应用
java·开发语言·笔记