免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1286 | 回复: 4
打印 上一主题 下一主题

回复关于Reflection API [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-12-12 20:58 |只看该作者 |倒序浏览
bingziyu_0贴了一张贴问及Reflection,我回了贴,bingziyu_0又问到:"与多态有何区别".我觉得回答这个问题需要比较长的篇幅,所以写了这张贴子,希望能解决sb的疑问. 我将根据我的经验,应用user case来阐述Reflection API 的应用.

利用多态让JVM在RUN TIME时决定调用那个类的METHOD.这些类之间是有关系的,它们是在同一个类树中.例如你的程序让用户选择要做什么,然后根据用户选择来决定要怎什做.你可能会有这些类.

public class UserCanSelect{
        public static void main(String[] args) {
                Actor _actor=null;
                System.out.println("dear user, what would you like to do next?");
                System.out.println("please select from one of the following options.");
                //1. KickBush'sAss.  //踢Bush的屁股 (应该的)
                //2. GambleWithChowYunFat //与赌神赌博 (找死嘛)
                //3. KissZhouXun //可能吗?
               
                if (userSelection==1) _actor=new Terror();
                else if (userSelection==2) _actor=new Gambler();
                else if (userSelection==3) _actor=new Lover();
                //Now do the job.
                _actor.action();  //这里由JVM在RUN TIME时决定调用那个类的METHOD
        }
}

public abstract class Actor{
        // do sth here

        public abstract void action(); //this really do the job.只是演戏而己,不是真的.
}

public class Terror extends Actor {
        public void action() {
                //stand in front of White House, you shout:
                //"Bush, Asshole, Get Out!"
                //Bush coming out, very scare.
                //"Turn around", you order him.
                //Now be quick and with strength
                //kick his ass.
                //job done!
        }
}

public class Gambler extends Actor {
        /**
         * I don't recommend this, because it's unlikely you're gonna win.
         */
        public void action() {
                //deal,
                //you get 21,
                //Now you smile, and you think you're gonna win
                //well, unfortunely, ChowYunFat gets BlackJack
                //You Lose!
        }
}

public class Lover extends Actor() {
        /**
         * This is disgusting, isn't it?
         * Person under 18 please keep away!
         */
        public void action() {
                //you are watching TV
                //you see ZhouXun on the TV
                //you come close to your TV, and
                //......
                //help!
                //i don't know how to do this
                //you faint
                //sos
        }
}

同样上面的例子,你用下面的class 来实现.
public class UserIsGod{
        public static void main(String[] args) {
                Performer _performer=new Performer();
                System.out.println("dear user, what would you like to do next?");
                System.out.println("please select from one of the following options.");
                //1. kickBushsAss.  //踢Bush的屁股 (应该的)
                //2. gambleWithChowYunFat //与赌神赌博 (找死嘛)
                //3. kissZhouXun //可能吗?
               
                if (userSelection.equals("kickBushsAss")) {
                        _performer.kickBushsAss();
                }
                else if (userSelection.equals("gambleWithChowYunFat")) {
                        _performer.gambleWithChowYunFat();
                }
                else if (userSelection.equals("kissZhouXun"))  {
                        _performer.kissZhouXun();
                }
        }
}

但是你的项目管理人是一个怪人,他觉得用很多if-else写程序让人觉得很懒.他想让他的程序有一种magician那样的魔力.你回家去,休息不了.你还在想着怎么解决这个问题.突然你想到Reflection API就是magician.所以你把上面的类改成下面的样子.
public class UserIsGod{
        public static void main(String[] args) {
                Performer _performer=new Performer();
                System.out.println("dear user, what would you like to do next?");
                System.out.println("please select from one of the following options.");
                //1. kickBushsAss.  //踢Bush的屁股 (应该的)
                //2. gambleWithChowYunFat //与赌神赌博 (找死嘛)
                //3. kissZhouXun //可能吗?
                String userSelection="";
                //get user selection here
                try {
                        Class _class=Class.forName("Performer");
                        Method _method=_class.getDeclaredMethod(userSelection,null);
                        Object _instance=_class.newInstance();
                        Object result=_method.invoke(_instance, null); //here your job is done
                }catch (Exception e) {}
        }
}

public class Performer {
        //something here

        public void kissBushsAss() {
                //stand in front of White House, you shout:
                //"Bush, Asshole, Get Out!"
                //Bush coming out, very scare.
                //"Turn around", you order him.
                //Now be quick and with strength
                //kick his ass.
                //job done!
        }

        /**
         * I don't recommend this, because it's unlikely you're gonna win.
         */
        public void gambleWithChowYunFat() {
                //deal,
                //you get 21,
                //Now you smile, and you think you're gonna win
                //well, unfortunely, ChowYunFat gets BlackJack
                //You Lose!
        }
        /**
         * This is disgusting, isn't it?
         * Person under 18 please keep away!
         */
        public void kissZhouxun() {
                //you are watching TV
                //you see ZhouXun on the TV
                //you come close to your TV, and
                //......
                //help!
                //i don't know how to do this
                //you faint
                //sos
        }
}
好了,你可以让你的project manager去吓唬他的客户了.你也可以去拿奖金了.
在sun的Reflection API Documentation 中写到
the API can be used to:
* construct new class instances and new arrays
* access and modify fields of objects and classes
* invoke methods on objects and classes
* access and modify elements of arrays
The core Reflection API 包括下列类:Method, Field, Constructor. 再加上Class(java.lang).大家回去看看这些类的documentation,就会明白了.
最后补充一句如果你的项目管理人不是一个怪人,那就请慎用Reflection吧.它会占用很多资源的.

论坛徽章:
0
2 [报告]
发表于 2003-12-13 11:19 |只看该作者

回复关于Reflection API

能不能讲一下Reflection为什么会占用很多资源呢?谢谢。

论坛徽章:
0
3 [报告]
发表于 2003-12-13 13:22 |只看该作者

回复关于Reflection API

Really wonderful

Reflection是在运行时装载类。

论坛徽章:
0
4 [报告]
发表于 2003-12-13 20:04 |只看该作者

回复关于Reflection API

》》Reflection为什么会占用很多资源
我想其中一个原因是你把应在编译时便应完成的工作放到了RUN TIME时。JVM就应该多付出了时间和内存去完成这个任务。

》》Reflection是在运行时装载类。

Reflection enables Java code to discover information about the fields, methods and constructors of loaded classes, and to use reflected fields, methods, and constructors to operate on their underlying counterparts on objects, within security restrictions. The API accommodates applications that need access to either the public members of a target object (based on its runtime class) or the members declared by a given class.

最常见的Reflection例子是 Java Beans。
我自己应用Reflection是为了程序具有通用性。
bingziyu_0 该用户已被删除
5 [报告]
发表于 2003-12-14 19:39 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP