黑马程序员技术交流社区

标题: 能不能通过,反射拿到父类的公共字段? [打印本页]

作者: 刘海陆    时间: 2013-6-1 19:36
标题: 能不能通过,反射拿到父类的公共字段?
能不能通过,反射拿到父类的公共字段?
作者: 画饼    时间: 2013-6-1 20:05
import java.lang.reflect.Field;

public class A     //演示成都春熙路上,窈窕淑女,方丈好球
{
        public static void main(String[] args) throws SecurityException, NoSuchFieldException
        {
                IPrettyGirls pg=new PrettyGirl("安娜");
                Men man=new AMan(pg,"智障禅师");
                pg.goodlooks();
                pg.goodshapes();
                pg.goodtemperament();
                pg.callloudly();
                man.findgirl();
//        打开注释        Field fi =AMan.class.getSuperclass().getField("prettygirl");
//                System.out.println(fi);
        }
}

interface IPrettyGirls            //的标准接口
{
        public void goodlooks();       //相貌好
        public void goodshapes();      //身材好
        public void goodtemperament(); //脾气好
        public void callloudly();      //不挑逗也不算美女
}

class PrettyGirl implements IPrettyGirls   //接口实现类
{
        protected static String name;            //定义字符串
        public PrettyGirl(String name)   //构造函数初始化美女名字
        {
                this.name =name;
        }
        public void goodlooks()         //重写接口方法
        {
                System.out.println("我好漂亮啊!!");  
        }
        public void goodshapes()        //重写接口方法
        {
                System.out.println("我好苗条啊!!");  
        }
        public void goodtemperament()      //重写接口方法
        {
                System.out.println("我好温柔啊!!");  
        }
        public void callloudly()          //重写接口方法
        {
                System.out.println("谁来娶我呀!!");
        }
}

abstract class Men            //人抽象类
{
        public IPrettyGirls prettygirl;
        static String name;
        public Men(IPrettyGirls prettygirl,String name)    //传入一个美女引用,让男人一开始就有追求目标
        {
                this.prettygirl=prettygirl;
                this.name=name;
        }
        public abstract void findgirl();        //追求女人
}

class AMan extends Men     //人扩展类
{
       
        public AMan(IPrettyGirls prettygirl,String name)
        {
                super( prettygirl,name);
               
        }
        public void findgirl()
        {
                System.out.println(AMan.name+"从容道:"+PrettyGirl.name+"不要喊别人了,你就从了老衲吧!!");
        }
}



作者: 画饼    时间: 2013-6-1 20:06
注释那里,你可以看看,我用反射调用父类的字段
//        打开注释        Field fi =AMan.class.getSuperclass().getField("prettygirl");
//                System.out.println(fi);
把这个打开,把文字去掉,上面那个类是一个朋友写的
作者: peerless2012    时间: 2013-6-1 21:17
  1. class  
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 ReflectPoint pt1=new ReflectPoint(1, 4);

  6.                 Field fieldY=ReflectPoint.class.getField("y");//获取公共字段
  7.                 int y = fieldY.getInt(pt1);//获取某个对象上的字段值
  8.                 System.out.println(y);
  9.                
  10.                 Field fieldX=ReflectPoint.class.getDeclaredField("x");
  11.                 fieldX.setAccessible(true);//因为x是ReflectPoint中的私有字段,需要暴力反射
  12.                 int x=fieldX.getInt(pt1);
  13.                 System.out.println(x);
  14.         }
  15. }
  16. public class ReflectPoint {
  17.         private int x;
  18.         public int y;

  19.                 public ReflectPoint(int x,int y) {
  20.                 super();
  21.                 this.x=x;
  22.                 this.y=y;
  23.         }

  24. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2