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');
		
		
	}
}
相关推荐
咖啡教室2 小时前
java日常开发笔记和开发问题记录
java
咖啡教室2 小时前
java练习项目记录笔记
java
鱼樱前端3 小时前
maven的基础安装和使用--mac/window版本
java·后端
RainbowSea3 小时前
6. RabbitMQ 死信队列的详细操作编写
java·消息队列·rabbitmq
RainbowSea3 小时前
5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
java·消息队列·rabbitmq
我不会编程5555 小时前
Python Cookbook-5.1 对字典排序
开发语言·数据结构·python
李少兄5 小时前
Unirest:优雅的Java HTTP客户端库
java·开发语言·http
此木|西贝5 小时前
【设计模式】原型模式
java·设计模式·原型模式
可乐加.糖5 小时前
一篇关于Netty相关的梳理总结
java·后端·网络协议·netty·信息与通信
无名之逆5 小时前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust