整理了一下java的反射用法,这是第一部分,针对构造函数的反射。
package com.xxx;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
public class Demo1 {
@Test
public void test1() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
Class c1=Class.forName("com.xxx.SwordBean");
Constructor con= c1.getConstructor();
SwordBean sw=(SwordBean) con.newInstance();
System.out.println(sw.getAttack());
}
@Test
public void test2() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
Class c1=Class.forName("com.xxx.SwordBean");
Constructor con=c1.getConstructor(float.class);
SwordBean sw=(SwordBean)con.newInstance(1200);
System.out.println("Price of this Sword is "+ sw.getPrice());
}
@Test
public void test3() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
Class c1=Class.forName("com.xxx.SwordBean");
Constructor con=c1.getConstructor(float.class,float.class);
SwordBean sw=(SwordBean)con.newInstance(1200,60);
System.out.println("This Sword costs "+ sw.getPrice()+", and attack power is "+sw.getAttack());
}
@Test
public void test4() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
Class c1=Class.forName("com.xxx.SwordBean");
Constructor con=c1.getConstructor(PropertyBean.class);
SwordBean sw=(SwordBean)con.newInstance(new PropertyBean());
PropertyBean pb=sw.getPropertyOfSword();
System.out.println(
"file: "+pb.getFire()+"\n"+
"water: "+pb.getWater()+"\n"+
"wind: "+pb.getWind()+"\n"+
"rock: "+pb.getRock()+"\n"+
"thunder: "+pb.getThunder()+"\n"
);
}
@Test
public void test5() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
Class c1=Class.forName("com.xxx.SwordBean");
Constructor con=c1.getDeclaredConstructor(String.class);
con.setAccessible(true);
SwordBean sw=(SwordBean)con.newInstance("White Slip");
System.out.println("Name of this Sword is "+ sw.getSwordName());
}
}
其中test5是针对的私有构造函数的反射,这里需要注意的是在实例化之前需要设置访问权限。
package com.xxx;
public class SwordBean {
String swordName;
float price;
float Attack;
int sid;
PropertyBean propertyOfSword;
public String getSwordName() {
return swordName;
}
public void setSwordName(String swordName) {
this.swordName = swordName;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public float getAttack() {
return Attack;
}
public void setAttack(float attack) {
Attack = attack;
}
private int getSid() {
return sid;
}
private void setSid(int sid) {
this.sid = sid;
}
public PropertyBean getPropertyOfSword() {
return propertyOfSword;
}
public void setPropertyOfSword(PropertyBean propertyOfSword) {
this.propertyOfSword = propertyOfSword;
}
public SwordBean() {
setAttack(1);
}
public SwordBean(float p) {
setPrice(p);
}
public SwordBean(float p, float a) {
setAttack(a);
setPrice(p);
}
public SwordBean(PropertyBean pro) {
setPropertyOfSword(pro);
}
private SwordBean(String Name)
{
setSwordName(Name);
}
}
package com.xxx;
public class PropertyBean {
public int getFire() {
return fire;
}
public void setFire(int fire) {
this.fire = fire;
}
public int getWater() {
return water;
}
public void setWater(int water) {
this.water = water;
}
public int getWind() {
return wind;
}
public void setWind(int wind) {
this.wind = wind;
}
public int getRock() {
return rock;
}
public void setRock(int rock) {
this.rock = rock;
}
public int getThunder() {
return thunder;
}
public void setThunder(int thunder) {
this.thunder = thunder;
}
int fire;
int water;
int wind;
int rock;
int thunder;
public PropertyBean()
{
setFire(0);
setWater(0);
setWind(0);
setRock(0);
setThunder(0);
}
}