求解一道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());
}
} 展开
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());
}
} 展开
1个回答
展开全部
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 那么就需要每个对象 赋值。现在基本满足你的要求了。
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 那么就需要每个对象 赋值。现在基本满足你的要求了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询