权限问题,下面的都不能访问到上面的reflectPoint方法:- package first;
 
  
- import java.lang.reflect.Constructor;
 
 - import java.lang.reflect.InvocationTargetException;
 
  
- class ReflectPoint{
 
 -         public int x;
 
 -         public int y;
 
 -         private String str1="123";
 
 -         private String str2="zhangsan";
 
 -         private String str3="lisi";
 
 -         public ReflectPoint(int x, int y)
 
 -         {
 
 -                 this.x=x;
 
 -                 this.y=y;
 
 -         }
 
 - //        公有一个转字符串的方法
 
 -         public String toString()
 
 -         {
 
 -                 return str1+":"+str2+":"+str3;                
 
 -         }        
 
 - }
 
 - public class Test{
 
 -         public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
 
 -                 Constructor constructor1=ReflectPoint.class.getConstructor(int.class,int.class);
 
 -                 ReflectPoint rp=(ReflectPoint) constructor1.newInstance(3,5);
 
 -                 System.out.println(rp);
 
 -         }
 
 - }
 
  复制代码 
改成这样就可以了 |