java反射專題三

作者:殘陽丶

轉自: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
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章