面向对象程序设计之封装性、继承性、多态性

面向控制台的计算器一

package com.test.java.features1;

import java.util.Scanner;

/**
 * 计算器类
 */
public class ComputingCounter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入 stop 关闭计算器");
        while (true) {
            System.out.println("请输入一个数");
            String number = scanner.nextLine();
            if (number.equals("stop")) { break;}
            System.out.println("请输入一个运算符号");
            String symbol = scanner.nextLine();
            if (number.equals("stop")) { break;}
            System.out.println("请输入一个数");
            String number2 = scanner.nextLine();
            if (number.equals("stop")) { break;}
            compute(Double.parseDouble(number), Double.parseDouble(number2), symbol);
        }
        scanner.close();
    }

    private static void compute(double number, double number2, String symbol) {
        switch (symbol) {
            case "+":
                System.out.println((number + number2));
                break;
            case "-":
                System.out.println((number - number2));
                break;
            case "*":
                System.out.println((number * number2));
                break;
            case "/":
                System.out.println((number / number2));
                break;
        }
    }
}
View Code

代码都写在了一个main方法里,这是面向过程编程。


面向控制台的计算器二

假如以后需求发生变化,需要写一个面向windows的计算器,计算器一的代码复用性不高;
所以,我们把计算器一的代码封装为两部分——Client和Opration;Client只负责输入输出,Opration负责后台计算。
这样,如果真的需求变化,要求写一个面向windows的计算器,Opration部分的代码完全是可以被复用的。

package com.test.java.features2;

import com.test.java.features2.Opration;

import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入 stop 关闭计算器");
        while (true) {
            System.out.println("请输入一个数");
            String number = scanner.nextLine();
            if (number.equals("stop")) { break;}
            System.out.println("请输入一个运算符号");
            String symbol = scanner.nextLine();
            if (number.equals("stop")) { break;}
            System.out.println("请输入一个数");
            String number2 = scanner.nextLine();
            if (number.equals("stop")) { break;}
            Opration.compute(Double.parseDouble(number), Double.parseDouble(number2), symbol);
        }
        scanner.close();
    }
}
Client.java
package com.test.java.features2;

public class Opration {
    public static void compute(double number, double number2, String symbol) {
        switch (symbol) {
            case "+":
                System.out.println((number + number2));
                break;
            case "-":
                System.out.println((number - number2));
                break;
            case "*":
                System.out.println((number * number2));
                break;
            case "/":
                System.out.println((number / number2));
        }
    }
}
Opration.java

面向控制台的计算器三

现在我们只支持加、减、乘、除,但是以后可能需要扩展其它的运算,比如x的y次方。
所以把可能发生变化的部分再次封装,分别封装为加法类、减法类、乘法类、除法类,并让它们共同继承Opration类,也就是定义了一个算法族;并使用简单工厂模式创建具体的类而返回抽象类型(多态

package com.test.java.features3;

import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入 stop 关闭计算器");
        while (true) {
            System.out.println("请输入一个数");
            String number = scanner.nextLine();
            if (number.equals("stop")) { break;}
            System.out.println("请输入一个运算符号");
            String symbol = scanner.nextLine();
            if (number.equals("stop")) { break;}
            System.out.println("请输入一个数");
            String number2 = scanner.nextLine();
            if (number.equals("stop")) { break;}
            Opration.passOn(Double.parseDouble(number), Double.parseDouble(number2), symbol);
        }
        scanner.close();
    }
}
Client.java
package com.test.java.features3;

public abstract class Opration {

    public static void passOn(double number, double number2, String symbol) {
        Opration opratoin = OprationFactory.getOpratoin(symbol);
        opratoin.compute(number, number2);
    }

    public abstract void compute(double number, double number2);
}
Opration.java
package com.test.java.features3;

public class Add extends Opration {
    @Override
    public void compute(double number, double number2) {
        System.out.println((number + number2));
    }
}
Add.java
package com.test.java.features3;

public class Sub extends Opration{
    @Override
    public void compute(double number, double number2) {
        System.out.println((number - number2));
    }
}
Sub.java
package com.test.java.features3;

public class Multiply extends Opration {
    @Override
    public void compute(double number, double number2) {
        System.out.println((number * number2));
    }
}
Multiply.java
package com.test.java.features3;

public class Divide extends Opration {
    @Override
    public void compute(double number, double number2) {
        System.out.println((number / number2));
    }
}
Divide.java
package com.test.java.features3;

public class OprationFactory {
    public static Opration getOpratoin(String symbol) {
        switch (symbol) {
            case "+":
                return new Add();
            case "-":
                return new Sub();
            case "*":
                return new Multiply();
            case "/":
                return new Divide();
        }
        return null;
    }
}
OprationFactory.java

总结:面向对象三大特征,封装性、继承性、多态性。有利于程序的复用性、可扩展性、维护性。

 

转载于:https://www.cnblogs.com/Mike_Chang/p/10137860.html

你可能感兴趣的:(面向对象程序设计之封装性、继承性、多态性)