直接上代码!源码面前,了无秘密!
Test.java
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
public class Test
{
public static void main(String[] args)
{
try
{
String echoVal = "";
//
//InsidePack insidePack = new InsidePack();
//BeanUtils.setProperty(insidePack, "x", 12);//异常
//echoVal = BeanUtils.getProperty(insidePack, "x");//异常
//System.out.println("InsidePack" + echoVal);
//
//PriSubClass priSubClass = new PriSubClass();
//BeanUtils.setProperty(priSubClass, "x", 24);//异常
//echoVal = BeanUtils.getProperty(priSubClass, "x");//异常
//System.out.println("PriSubClass" + echoVal);
//
//ProSubClass proSubClass = new ProSubClass();
//BeanUtils.setProperty(proSubClass, "x", 36);//异常
//echoVal = BeanUtils.getProperty(proSubClass, "x");//异常
//System.out.println("ProSubClass" + echoVal);
//
PubSubClass pubSubClass = new PubSubClass();
BeanUtils.setProperty(pubSubClass, "x", 48);
echoVal = BeanUtils.getProperty(pubSubClass, "x");
System.out.println("PubSubClass" + echoVal);
//
Outer outer = new Outer();
BeanUtils.setProperty(outer, "x", 60);
echoVal = BeanUtils.getProperty(outer, "x");
System.out.println("Outer" + echoVal);
//
//OutPack outPack = new OutPack();
//BeanUtils.setProperty(outPack, "x", 72);//异常
//echoVal = BeanUtils.getProperty(outPack, "x");//异常
//System.out.println("OutPack" + echoVal);
}
catch(IllegalAccessException e)
{
e.printStackTrace();
}
catch(InvocationTargetException e)
{
e.printStackTrace();
}
catch(NoSuchMethodException e)
{
e.printStackTrace();
}
}
private static class PriSubClass
{
private int x;
public int getX()
{
return x;
}
public void setX(int x)
{
this.x = x;
}
}
protected static class ProSubClass
{
private int x;
public int getX()
{
return x;
}
public void setX(int x)
{
this.x = x;
}
}
public static class PubSubClass
{
private int x;
public int getX()
{
return x;
}
public void setX(int x)
{
this.x = x;
}
}
}
class InsidePack
{
private int x;
public int getX()
{
return x;
}
public void setX(int x)
{
this.x = x;
}
}
Outer.java
public class Outer
{
private int x;
public int getX()
{
return x;
}
public void setX(int x)
{
this.x = x;
}
}
class OutPack
{
private int x;
public int getX()
{
return x;
}
public void setX(int x)
{
this.x = x;
}
}
上述代码中,标注为异常的即注意点~~~