一个计算器,部分代码如下 ,就是比如:1+3=4....等等内容
现在要把它改成面向对象,应该怎么改的。
要求可维护性,比如: 我本来是判断1到5的,现在我想加入6到7的,应该怎么修改的最好能减少if语句,if(e.getActionCommand()=="1"){
if(b!=5){
s=s+"1";
text.setText(s);
}
}
if(e.getActionCommand()=="2"){
if(b!=5){
s=s+"2";
text.setText(s);
}
}
if(e.getActionCommand()=="3"){
if(b!=5){
s=s+"3";
text.setText(s);
}
}
if(e.getActionCommand()=="4"){
if(b!=5){
s=s+"4";
text.setText(s);
}
}
if(e.getActionCommand()=="5"){
if(b!=5){
s=s+"5";
text.setText(s);
}
}
现在要把它改成面向对象,应该怎么改的。
要求可维护性,比如: 我本来是判断1到5的,现在我想加入6到7的,应该怎么修改的最好能减少if语句,if(e.getActionCommand()=="1"){
if(b!=5){
s=s+"1";
text.setText(s);
}
}
if(e.getActionCommand()=="2"){
if(b!=5){
s=s+"2";
text.setText(s);
}
}
if(e.getActionCommand()=="3"){
if(b!=5){
s=s+"3";
text.setText(s);
}
}
if(e.getActionCommand()=="4"){
if(b!=5){
s=s+"4";
text.setText(s);
}
}
if(e.getActionCommand()=="5"){
if(b!=5){
s=s+"5";
text.setText(s);
}
}
解决方案 »
- Java SQL批处理异常,如何得到出问题的那条sql信息
- 【新手学java】jdk1.6+myeclipse6.5+sqlserver2000数据库连接问题
- 帮我一下哈!
- 求救:关于Java自动输入的问题(万分紧急!)
- 我在Eclipse中编译提示“java.io.FileNotFoundException,系统找不到指定的文件”,但是在dos命令下就可以不可以
- 有关JCreator的小问题请大家帮忙
- j2re和jsdk有什么区别啊?
- java中怎样读取应用程序名
- Jbuilder 新手遇到难题啦!
- 其实给应该得到分数的人给分也是很快乐的一件事
- 有个关于JAVA学习的疑问,请教各位JAVA武林高手?
- ? - URLConnection.getExpiration()是什么意思
if (e.getActionCommand() == strNum) {
if (b != 5) {
s = s + strNum;
text.setText(s);
}
}