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 小时前
C#基础11-常用类
android·java·c#
C嘎嘎嵌入式开发2 小时前
(2)100天python从入门到拿捏
开发语言·python
Stanford_11062 小时前
如何利用Python进行数据分析与可视化的具体操作指南
开发语言·c++·python·微信小程序·微信公众平台·twitter·微信开放平台
小许学java3 小时前
数据结构-ArrayList与顺序表
java·数据结构·顺序表·arraylist·线性表
Vallelonga3 小时前
Rust 中的数组和数组切片引用
开发语言·rust
Kiri霧3 小时前
Rust模式匹配详解
开发语言·windows·rust
white-persist4 小时前
Python实例方法与Python类的构造方法全解析
开发语言·前端·python·原型模式
千里马-horse4 小时前
Async++ 源码分析8--partitioner.h
开发语言·c++·async++·partitioner
Java 码农4 小时前
Centos7 maven 安装
java·python·centos·maven
harmful_sheep4 小时前
maven mvn 安装自定义 jar 包
java·maven·jar