java反射专题三

本文深入探讨Java反射机制,展示如何通过反射调用类的属性、方法及构造器,包括访问私有成员和调用静态方法,是理解Java动态特性的关键。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作者:残阳丶

转自:https://www.cnblogs.com/cainiao-Shun666/p/8568763.html

一丶调用运行时类中指定的属性

父类:

public abstract class Person {

	public String personName;
        int personAge;
	
	public void play(){
	}
	
	void learn(){
	}
	
}

子类:

public class HelloPerson extends Person {

	public String helloPersonName;
	int helloPersonAge;
	public String helloPersonWeight;
	private String helloPersonHeight;
	protected String sex;

	public HelloPerson() {
	}
	
	public HelloPerson(String helloPersonName, int helloPersonAge) {
		this.helloPersonName = helloPersonName;
		this.helloPersonAge = helloPersonAge;
	}

	public void sayHello(String msg){
		System.out.println(msg);
	}
	
	private void sayGoodBye(){
		System.out.println("goodBye");
	}

	public static void staticTest(){
		System.out.println("static method...");
	}
	
	public String getHelloPersonName() {
		return helloPersonName;
	}

	public int getHelloPersonAge() {
		return helloPersonAge;
	}
	
}

测试类:

import java.lang.reflect.Field;
import com.reflect.HelloPerson;

public class NioTest4 {

	public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException
		, SecurityException, InstantiationException, IllegalAccessException {
		Class clazz = HelloPerson.class;
		//1.获取指定的属性
		Field personName = clazz.getField("personName");//获取运行时类及其父类中声明public的指定名为personName的属性
		//2.创建运行时类的对象
		HelloPerson person = (HelloPerson)clazz.newInstance();
		//3.将运行时类的指定属性赋值
		personName.set(person, "name1");
		Field helloPersonAge = clazz.getDeclaredField("helloPersonAge");//可以获取运行时类中指定名为helloPersonAge的属性
		helloPersonAge.setAccessible(true);//由于属性权限修饰符的限制,为了保证可以给属性赋值,需要在设置属性前使得有权限操作该属性
		helloPersonAge.set(person, 19);
		System.out.println(person.getHelloPersonAge());
		System.out.println(person.personName);
	}
}

结果:

19
name1

如果将上面8、12行的代码改为:

Class clazz = Person.class;
Person person = (Person)clazz.newInstance();

会报错:

Exception in thread "main" java.lang.InstantiationException
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)

二丶调用运行时类中指定的方法

 

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import com.reflect.HelloPerson;

public class NioTest4 {

	public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException
		, SecurityException, InstantiationException, IllegalAccessException, NoSuchMethodException
		, IllegalArgumentException, InvocationTargetException {
		Class clazz = HelloPerson.class;
		//1.获取运行时类中声明public的指定的方法
		Method method = clazz.getMethod("sayHello", String.class);
		HelloPerson person = (HelloPerson)clazz.newInstance();
		Object returnVal = method.invoke(person, "hello");
		//2.获取运行时类中指定的方法
		Method m = clazz.getDeclaredMethod("sayGoodBye");
		m.setAccessible(true);
		m.invoke(person);
		//3.获得静态的方法
		Method m1 = clazz.getMethod("staticTest");
		m1.invoke(clazz);
	}
}

结果:

hello
goodBye
static method...

 三丶调用指定的构造器

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import com.reflect.HelloPerson;

public class NioTest4 {

	public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException
		, SecurityException, InstantiationException, IllegalAccessException, NoSuchMethodException
		, IllegalArgumentException, InvocationTargetException {
		Class clazz = HelloPerson.class;
		Constructor c = clazz.getDeclaredConstructor(String.class, int.class);
		c.setAccessible(true);
		HelloPerson p = (HelloPerson)c.newInstance("xiaoXiao", 19);
		System.out.println(p.getHelloPersonName());
	}
}

结果:

xiaoXiao
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值