英文:
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'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
- 不要使用原生的名称,例如,你应该使用 
MyMath来避免与原生的Math发生冲突。 - 最好将 
MyMath声明为一个接口。请注意,一个类可以实现多个接口,但只能继承一个类。 - 在将新计算的值放入 
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
如有任何疑问或问题,请随时提问。
英文:
- Do not use a name which OOTB e.g you should use 
MyMathto avoid conflict with OOTBMath. - It's better to declare 
MyMathas ininterface. Note that multiple interfaces can be implemented by a class but only one class can be extended. - Assign the value of 
resulttoprevresultbefore 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论