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');
		
		
	}
}
相关推荐
blammmp4 分钟前
Java:数据结构-枚举
java·开发语言·数据结构
何曾参静谧17 分钟前
「C/C++」C/C++ 指针篇 之 指针运算
c语言·开发语言·c++
暗黑起源喵22 分钟前
设计模式-工厂设计模式
java·开发语言·设计模式
WaaTong27 分钟前
Java反射
java·开发语言·反射
Troc_wangpeng28 分钟前
R language 关于二维平面直角坐标系的制作
开发语言·机器学习
努力的家伙是不讨厌的30 分钟前
解析json导出csv或者直接入库
开发语言·python·json
Envyᥫᩣ44 分钟前
C#语言:从入门到精通
开发语言·c#
九圣残炎1 小时前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
wclass-zhengge1 小时前
Netty篇(入门编程)
java·linux·服务器
童先生1 小时前
Go 项目中实现类似 Java Shiro 的权限控制中间件?
开发语言·go