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');
		
		
	}
}
相关推荐
时光の尘5 分钟前
C语言菜鸟入门·关键字·float以及double的用法
运维·服务器·c语言·开发语言·stm32·单片机·c
paopaokaka_luck6 分钟前
[371]基于springboot的高校实习管理系统
java·spring boot·后端
以后不吃煲仔饭19 分钟前
Java基础夯实——2.7 线程上下文切换
java·开发语言
进阶的架构师20 分钟前
2024年Java面试题及答案整理(1000+面试题附答案解析)
java·开发语言
前端拾光者24 分钟前
利用D3.js实现数据可视化的简单示例
开发语言·javascript·信息可视化
The_Ticker25 分钟前
CFD平台如何接入实时行情源
java·大数据·数据库·人工智能·算法·区块链·软件工程
程序猿阿伟25 分钟前
《C++ 实现区块链:区块时间戳的存储与验证机制解析》
开发语言·c++·区块链
傻啦嘿哟43 分钟前
如何使用 Python 开发一个简单的文本数据转换为 Excel 工具
开发语言·python·excel
大数据编程之光1 小时前
Flink Standalone集群模式安装部署全攻略
java·大数据·开发语言·面试·flink
初九之潜龙勿用1 小时前
C#校验画布签名图片是否为空白
开发语言·ui·c#·.net