求解一道Java实验题,给出一段代码,要求把该代码补充完整使其可以运行,具体要求如下

packagexinguan.班级.姓名;//班级、姓名:请用真实班级和姓名的拼音替换abstractclassOperation{//抽象类publicstaticdo... packagexinguan.班级.姓名;//班级、姓名:请用真实班级和姓名的拼音替换 
abstractclassOperation{ //抽象类
public staticdoublenumberA= 0;
publicstaticdoublenumberB = 0;

abstractdouble getResult(); //抽象方法
}

class OperationFactory{
publicstatic OperationcreateOperate(charoperate){
Operationoper = null;
switch (operate){
case'+':
oper= newOperationADD();
break;
case'-':
oper= newOperationSUB();
break;

case'*':
oper= newOperationMUL();
break;

case'/':
oper= newOperationDIV();
break;

}
return oper;
}
}

publicclassCalculateDemo {

/**
* @param args
*/
publicstaticvoid main(String[] args) {
// TODO Auto-generated method stub
OperationoperADD = OperationFactory.createOperate('+');
OperationoperSUB = OperationFactory.createOperate('-');
Operation operMUL = OperationFactory.createOperate('*');
Operation operDIV = OperationFactory.createOperate('/');

operADD.numberA = 15.0;
operADD.numberB = 3;

System.out.println(operAdd.getResult());
System.out.println(operSUB.getResult());
System.out.println(operMULd.getResult());
System.out.println(operDIV.getResult());
}
}
展开
 我来答
aaagu1234
2015-11-26 · 超过78用户采纳过TA的回答
知道小有建树答主
回答量:149
采纳率:0%
帮助的人:101万
展开全部
package xinguan;
abstract class Operation{ //抽象类
public static double numberA= 0;
public static double numberB = 0;

abstract double getResult(); //抽象方法
}

class OperationADD extends Operation{

@Override
double getResult() {

return numberA+numberB;
}
}

class OperationSUB extends Operation{

@Override
double getResult() {

return numberA-numberB;
}
}
class OperationMUL extends Operation{

@Override
double getResult() {

return numberA*numberB;
}
}
class OperationDIV extends Operation{

@Override
double getResult() {

return numberA/numberB;
}
}

class OperationFactory{
public static Operation createOperate(char operate){
Operation oper = null;
switch (operate){
case'+':
oper= new OperationADD();
break;
case'-':
oper= new OperationSUB();
break;

case'*':
oper= new OperationMUL();
break;

case'/':
oper= new OperationDIV();
break;

}
return oper;
}
}
public class CalculateDemo {

/**
* @param args
*/
public static void main(String[] args) {

Operation operADD = OperationFactory.createOperate('+');
Operation operSUB = OperationFactory.createOperate('-');
Operation operMUL = OperationFactory.createOperate('*');
Operation operDIV = OperationFactory.createOperate('/');

operADD.numberA = 15.0;
operADD.numberB = 3;

System.out.println(operADD.getResult());
System.out.println(operSUB.getResult());
System.out.println(operMUL.getResult());
System.out.println(operDIV.getResult());
}
}
因为抽象类是静态方法 所以 给operADD 那个对象赋值一次就能获得所有结果。要是去掉static 那么就需要每个对象 赋值。现在基本满足你的要求了。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式