作者:残阳丶
一丶调用运行时类中指定的属性
父类:
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