如何在以下Java程序中引入封装?

时间:2016-08-07 16:04:41

标签: java oop encapsulation

我创建了以下Java程序。它的基本功能是对两个数字执行加法,减法,乘法,除法和模块化除法。

我已经实现了面向对象编程的概念,但缺少封装。

如何在其中引入封装?

我的代码是:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author piyali
 */

public class Calculator {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        // TODO code application logic here
        int x, y;
        x = 13;
        y = 5;

        calculation add = new calculation();
        calculation sub = new calculation();
        calculation mul = new calculation();
        calculation div = new calculation();
        calculation mod = new calculation();

        int addResult = add.addition(x, y);
        int subResult = sub.subtraction(x, y);
        int mulResult = mul.multiplication(x, y);
        int divResult = mul.division(x, y);
        int modResult = mod.modularDivision(x, y);

        System.out.println("The addition of the numbers is " +addResult);
        System.out.println("The subtraction of the two numbers is " +subResult);
        System.out.println("The multiplication of the two numbers is " + mulResult);
        System.out.println("The division of the two numbers is " +divResult);
        System.out.println("The modular division of the two numbers is " + modResult);
    }
}

class calculation {

    int addition(int x, int y){
        int z;
        z = x + y;
        return(z);
    }

    int subtraction(int x, int y){
        int z;
        z = x - y;
        return(z);
    }

    int multiplication(int x, int y){
        int z;
        z = x * y;
        return(z);
    }

    int division(int x, int y){
        int z;
        z = x / y;
        return(z);
    }

    int modularDivision(int x, int y){
        int z;
        z = x % y;
        return(z);
    }
}

3 个答案:

答案 0 :(得分:2)

如果您想要真正的OOP和封装,那么创建具有方法Calculation的接口int calculate()

public interface Calculation {

     int calculate();
}

现在创建实现此接口的类,例如AdditionSubtraction等。

public class Addition implements Calculation {

     private final int x;
     private final int y;

     public Addition(int x, int y) {
         this.x = x;
         this.y = y;
     }

     @Override
     public int calculate(){
         return x + y;
     }
 }

主要方法

public static void main(String[] args) {

    int x, y;
    x = 13;
    y = 5;

    Calculation add = new Addition(x, y);
    System.out.println(add.calculate());
}

此类设计的优点是,如果您想要添加任何额外的数学运算,例如root,百分比甚至派生,您将不需要修改任何类的实现。只需编写实现Calculation的额外类。

答案 1 :(得分:0)

我认为在这里封装很多东西。这看起来像是一个基本的数学库,并且因为它没有数据成员或帮助函数,所以你不能在这里做很多事情来封装。

由于创建此类的实例有点愚蠢(例如,请参阅Java Math库),因此请将方法设为静态,并将构造函数设为私有。

我想这不是一种封装形式,而是一些设计技巧:你应该制作你不会修改最终版本的变量。如果您不使用它们,也应该删除所有不必要的变量。他们没有为了可读性做任何事情,他们只是使用额外的内存块(除非JVM优化它们)。

改进代码:

class calculation {
    private calculation() {
        throw new RuntimeException("Don't instantiate a math library!");
    }

    public static int addition(final int x, final int y){
        return x + y;
     }

    public static int subtraction(final int x, final int y){
        return x - y;
    }

    public static int multiplication(final int x, final int y){
        return x * y;
    }

    public static int division(final int x, final int y){
        return x / y;
    }

    public static int modularDivision(final int x, final int y){
        return x % y;
    }
} 

通过这样做,现在您可以使用以下内容调用计算库:

int additionResult = calculation.addition(5, 5);
System.out.println(additionResult);

答案 2 :(得分:-3)

使用getter和setter的概念。

class Calculator{
private int x, y, z;

void setValue(int a, int b){
    x=a;
    y=b;
}

int getValue(){
    return z;
}

void addition(){
    z=x+y;
}

void subtraction(){
    z=x-y;
}

void multiplication(){
    z=x*y;
}

void division(){
    z=x/y;
}

void modDivision(){
    z=x%y;
}
}


public class CalculatorTestDrive {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    int num1, num2, result;

    num1=13;
    num2=5;

    Calculator add = new Calculator();
    Calculator sub = new Calculator();
    Calculator mul = new Calculator();
    Calculator div = new Calculator();
    Calculator mod = new Calculator();

    add.setValue(num1, num2);
    add.addition();
    result = add.getValue();
    System.out.println("The addition of " + num1 + " and " + num2 + " is " +result);

    sub.setValue(num1, num2);
    sub.subtraction();
    result = sub.getValue();
    System.out.println("The subtraction of " + num1 + " and " + num2 + " is " +result);

    mul.setValue(num1, num2);
    mul.multiplication();
    result = mul.getValue();
    System.out.println("The multiplication of " + num1 + " and " + num2 + " is " +result);

    div.setValue(num1, num2);
    div.division();
    result = div.getValue();
    System.out.println("The division of " + num1 + " and " + num2 + " is " +result);

    mod.setValue(num1, num2);
    mod.modDivision();
    result = mod.getValue();
    System.out.println("The modular division of " + num1 + " and " + num2 + " is " +result);
}

}