- 论坛徽章:
- 0
|
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吧.它会占用很多资源的. |
|