polymorphism. class A{ public void doSomething(){ ... } } class B extends A{ public void doSomething(){ super.doSomething(); .... } } class C extends B{ public void doSomething(){ super.doSomething(); ... } } 看情况决定怎么instantiate,写个factory也可以 A a = new A/B/C(); 然后真正干活的代码就好看了,只有一行,不同的逻辑用不同的子类封装: a.doSomething(); 搞定。
【在 B*********h 的大作中提到】 : polymorphism. : class A{ : public void doSomething(){ : ... : } : } : class B extends A{ : public void doSomething(){ : super.doSomething(); : ....
m*t
4 楼
What's wrong with that? Well, some people choose to do: if (A) { ... } if (A && B) { ... } if (A && B && C) { ... } But I don't like it for many reasons.
【在 B*********h 的大作中提到】 : polymorphism. : class A{ : public void doSomething(){ : ... : } : } : class B extends A{ : public void doSomething(){ : super.doSomething(); : ....
f*h
6 楼
我不太清楚if里面复合条件的执行顺序, 如果在if(A && B && C)这一句中,保证在任何java platform下面都是先evaluate A , 再b, 再c,我的问题就可以解决了。 我现在只是在xp 下用sun的 jdk 6, eclipse下面验证了 先a再b,没有验证c。
【在 m******t 的大作中提到】 : What's wrong with that? : Well, some people choose to do: : if (A) { : ... : } : if (A && B) { : ... : } : if (A && B && C) { : ...
g*g
7 楼
Nothing wrong with it, just get used to it. If it really bothers you, put it in a method and do if(...) { return; } if(....) { return; } ....
you can use state or strategy pattern to replace conditional sentences such as if or case, etc. The problem with conditional sentences is it is hard to extend. But if each branch of your conditional sentences is small, using them might be overskilled.
m*t
9 楼
Yes, it's guaranteed by the java spec. Any JVM that doesn't do that is utterly broken. I still would _not_ recommend this style - it has all sorts of problems: redundant evaluation, potential side effects, etc.