如何保存先前的结果

huangapple go评论64阅读模式
英文:

How to save the previous result

问题

我有一个任务

创建一个名为 Math 的类,具有以下方法:
int calculate(int a, int b) - 执行计算操作
以及
int returnPrev() - 返回之前计算的结果。


创建一个名为 Addition 的类来表示加法操作。重写这些方法。
我已经创建了一个抽象类 Math

```java
    abstract class Math {
        abstract int calculate(int a, int b);
        abstract int returnPrev();
    }
    
    
    public class Addition extends Math {
        int result;
        int prevResult;
    
        
        @Override
        int calculate(int a, int b) {
            result = a + b;
            prevResult = result; // 保存上一次计算的结果
            return result;
        }
        
        @Override
        int returnPrev() {
            return prevResult;
        }
    }

对于我来说,不清楚如何创建 returnPrev 方法?我不明白应该如何保存第一次的结果。

对于任何解释,我会非常感激


<details>
<summary>英文:</summary>

I have a task

Create a class Math with methods:
int calculate (int a, int b) - the operation itself
and
int returnPrev () - returns the previous calculated result.


Create class Addition to represent addition operations. Override the methods.
I&#39;ve created abstract class Math

abstract class Math {
    abstract int calculate(int a,int b);
    abstract int returnPrev();
}


public class Addition extends Math {
    int result;
    int prevresult;

    
    @Override
    int calculate(int a, int b) {
       result = a + b;
        return result   }
    @Override
    int returnPrevious() {
     }
}



For me is unclear how should i create a method returnPrevious ?? I do not understand how should i save the first result.



For any explanation would be very grateful





</details>


# 答案1
**得分**: 0

```java
public class Addition extends Math {
    int result;
    int prevresult;

    @Override
    int calculate(int a, int b) {
        this.prevresult = result;
        result = a + b;
        return result;
    }

    @Override
    int returnPrev() {
        return this.prevresult;
    }
}
英文:
public class Addition extends Math {
    int result;
    int prevresult;


    @Override
    int calculate(int a, int b) {
        this.prevresult = result;
        result = a + b;
        return result ;
    }

    @Override
    int returnPrev() {
        return this.prevresult;
    }

}

答案2

得分: 0

  1. 不要使用原生的名称,例如,你应该使用 MyMath 来避免与原生的 Math 发生冲突。
  2. 最好将 MyMath 声明为一个接口。请注意,一个类可以实现多个接口,但只能继承一个类。
  3. 在将新计算的值放入 result 之前,将 result 的值赋给 prevresult
interface MyMath {
    public int calculate(int a, int b);
    public int returnPrev();
}

class Addition implements MyMath {
    int result;
    int prevresult;

    @Override
    public int calculate(int a, int b) {
        prevresult = result;
        result = a + b;
        return result;
    }

    @Override
    public int returnPrev() {
        return prevresult;
    }
}

public class Demo {
    public static void main(String[] args) {
        Addition addition = new Addition();
        System.out.println(addition.calculate(10, 20));
        System.out.println(addition.returnPrev());
        System.out.println(addition.calculate(20, 30));
        System.out.println(addition.returnPrev());
    }
}

输出:

30
0
50
30

如有任何疑问或问题,请随时提问。

英文:
  1. Do not use a name which OOTB e.g you should use MyMath to avoid conflict with OOTB Math.
  2. It's better to declare MyMath as in interface. Note that multiple interfaces can be implemented by a class but only one class can be extended.
  3. Assign the value of result to prevresult before putting the value of new calculation into it.

interface MyMath {
	public int calculate(int a, int b);
	public int returnPrev();
}

class Addition implements MyMath {
	int result;
	int prevresult;

	@Override
	public int calculate(int a, int b) {
		prevresult = result;
		result = a + b;
		return result;
	}

	@Override
	public int returnPrev() {
		return prevresult;
	}
}

public class Demo {

	public static void main(String[] args) {
		Addition addition = new Addition();
		System.out.println(addition.calculate(10, 20));
		System.out.println(addition.returnPrev());
		System.out.println(addition.calculate(20, 30));
		System.out.println(addition.returnPrev());
	}
}

Output:

30
0
50
30

Feel free to comment in case of any doubt/issue.

huangapple
  • 本文由 发表于 2020年4月5日 03:50:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/61033907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定