标签:
import java.util.*; public class Test01{ // 测试类 public static void main(String [] args){ finalTest(); } public static void finalTest(){ while(true){
// 实例化一个计算类的对象,通过 set 方法设置值, get方法获取值 OperationTest p = new OperationTest(); p.setNum1(new Scanner(System.in).nextDouble()); String s= new Scanner(System.in).next(); p.setNum2(new Scanner(System.in).nextDouble()); Test(p.getNum1(),s,p.getNum2()); } } public static void Test(double num1,String e,double num2){ final String a ="+",b="-",c="*",d="/"; switch(e){ case a:System.out.println(num1+e+num2+"="+OperationAdd.getResult()); break; case b:System.out.println(num1+e+num2+"="+OperationSub.getResult()); break; case c:System.out.println(num1+e+num2+"="+OperationMul.getResult()); break; case d:System.out.println(num1+e+num2+"="+OperationDiv.getResult()); break; } } } // 计算类的父类 class OperationTest{ public static double num1; public static double num2; // get set 方法 public OperationTest() { } public double getNum1() { return num1; } public double getNum2() { return num2; } public void setNum1(double num1) { this.num1 = num1; } public void setNum2(double num2) { this.num2 = num2; } public static double getResult(){ double result =0; return result; } } // 计算加法的类 继承 自计算类 class OperationAdd extends OperationTest{ public static double getResult(){ return num1+num2; } } // 计算减法的类 继承自计算类 class OperationSub extends OperationTest{ public static double getResult(){ return num1-num2; } } // 计算乘法的类 继承自计算类 class OperationMul extends OperationTest{ public static double getResult(){ return num1*num2; } } // 计算除法的类 继承自计算类 class OperationDiv extends OperationTest{ public static double getResult(){ return num1/num2; } }
标签:
原文地址:http://www.cnblogs.com/suowei/p/4875582.html