A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 钟成军 高级黑马   /  2012-6-7 01:18  /  2179 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

学习张老师的视到“成员变量的反射”这一节时,发现问题,百度找了也没有找出来,求助!

public class ReflectPoint {
private int x ;
private int y;

public ReflectPoint(int x, int y) {
  super();
  this.x = x;
  this.y = y;
}

}


public class ReflectTest {
public static void main(String[] args) throws Exception {
  
  ReflectPoint pt1 = new ReflectPoint(3,5);
  Field fieldY = pt1.getClass().getField("y");
/*这里为什么张老师编译运行都正常,我编译时却报错?“Field cannot be resolved to a type”*/
  Syetem.out.println(fieldY.get(pt1));
}
}
刚学会玩Eclipse,是不是与设置有关,还是什么问题,请哪位大哥解答一下,谢谢!


评分

参与人数 1技术分 +1 收起 理由
袁錦泰 + 1

查看全部评分

7 个回复

倒序浏览
getDeclaredField方法才能够访问私有成员,你调用的方法只能访问public的成员
张老师讲的过程中定义了一个public和一个private,lz看视频不仔细哦。
回复 使用道具 举报
反射有3种
getfield方法只能拿到有访问权限的字段
getDeclaredField 方法可以拿到所有声明的字段,但需要setAccessable(true),这包括final | private | static的
static final同时修饰的字段无法被反射
回复 使用道具 举报
getFiled只能访问共有的,私
  1. package zhangxiao;

  2. import java.lang.reflect.*;

  3. public class ReflectTest {
  4. public static void main(String[] args) throws Exception {
  5.   
  6.   ReflectPoint pt1 = new ReflectPoint(3,5);
  7.   Field fieldY = pt1.getClass().getDeclaredField("y");//getFiled只能访问共有的,私有的可以用getDeclaredField方法,
  8. /*这里为什么张老师编译运行都正常,我编译时却报错?“Field cannot be resolved to a type”*/
  9. fieldY.setAccessible(true);
  10.   System.out.println(fieldY.get(pt1));
  11. }
  12. }

  13. public class ReflectPoint {
  14. private int x ;
  15. private int y;

  16. public ReflectPoint(int x, int y) {
  17.   super();
  18.   this.x = x;
  19.   this.y = y;
  20. }

  21. }
复制代码
有的可以用getDeclaredField方法,还有你的打印语句写错了,System。改后代码如下:
回复 使用道具 举报
Field fieldY=pt1.getClass().getDeclaredField("y");     //y是private的 getDeclaredField不管private还是public,还是protected都会拿到
fieldY.setAccessible(true);//暴力反射 设置可见
System.out.println(fieldY.get(pt1));//3
回复 使用道具 举报
本帖最后由 钟成军 于 2012-6-7 09:57 编辑

试了上面几位哥们的方法,把视频再看了一下,还是报同样的错误,不知怎么回事??
package my_pak1;
public class ReflectPoint {
private int x ;
public   int y;


public ReflectPoint(int x, int y) {
  super();
  this.x = x;
  this.y = y;
}

}//这里没有错。

package my_pak1;
import java.lang.reflect.*;
public class ReflectTest {
  
public static void main(String[] args) throws Exception {



  ReflectPoint pt1 = new ReflectPoint(3,5);//这里还是这个错?“Field cannot be resolved to a type”
  
  Field fieldY = pt1.getClass().getField("y");
  
  
  System.out.println(fieldY.get(pt1));

}
}


回复 使用道具 举报
钟成军 发表于 2012-6-7 09:53
试了上面几位哥们的方法,把视频再看了一下,还是报同样的错误,不知怎么回事??
package my_pak1;
public ...

import java.lang.reflect.Field;

public class ReflectPoint
{
       
        private int x;
       
        private int y;
       
        public ReflectPoint(int x, int y)
        {
       
                super();
                this.x = x;
                this.y = y;
        }
       
}// 这里没有错。

class ReflectTest
{
       
        public static void main(String[] args) throws Exception
        {
       
                ReflectPoint pt1 = new ReflectPoint(3, 5);// 这里还是这个错?“Field cannot be
                                                                                                  // resolved to a type”
               
                Field fieldY = pt1.getClass().getDeclaredField("y");
                fieldY.setAccessible(true);
                System.out.println(fieldY.get(pt1));
               
        }
}
回复 使用道具 举报
刘蕴学 发表于 2012-6-7 11:15
import java.lang.reflect.Field;

public class ReflectPoint

谢谢,已解决!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马