本帖最后由 杜成龙 于 2013-10-17 16:05 编辑
- package cn.itcast.day1;
- public class Person {
- int x;
- int y;
- public Person(int x, int y) {
- this.x = x;
- this.y = y;
- }
- }
复制代码- package cn.itcast.day1;
- import java.lang.reflect.Field;
- public class Demo {
- public static void main(String[] args)throws Exception {
- Person p=new Person(3,5);
- Field fy=p.getClass().getField("x");
- System.out.println(fy.get(p));
- }
复制代码 我运行主函数的时候,却显示:
Exception in thread "main" java.lang.NoSuchFieldException: x
at java.lang.Class.getField(Class.java:1520)
at cn.itcast.day1.Demo.main(Demo.java:9)
我上面的程序明明是按老师讲的写的啊,怎么就不对了呢?还望大家指教一下,谢谢!
|
|