java中的前置条件与后置条件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19119684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pre-condition vs Post-condition in java?
提问by
For example I have the following code:
例如,我有以下代码:
public class Calc(){
final int PI = 3.14; //is this an invariant?
private int calc(int a, int b){
return a + b;
//would the parameters be pre-conditions and the return value be a post-condition?
}
}
I am just confused on what exactly these terms mean? The code above is what I think it is, however can anyone point me into the right direction with my theory?
我只是对这些术语的确切含义感到困惑?上面的代码是我认为的那样,但是有人能用我的理论指出我正确的方向吗?
回答by Bucket
Just a word of warning, casting a floating-point number (3.14
) to an int
is going to leave you with trouble. You might want to cast it to a float:
只是警告一下,将浮点数 ( 3.14
) 转换为 anint
会给您带来麻烦。您可能希望将其转换为浮点数:
final float PI = 3.14f;
final
means that the variable can no longer be changed.
final
意味着不能再更改变量。
a
and b
are just parameters that you pass into calc()
. Before, they can be called whatever you want them to be, but inside calc()
you can refer to them as a
and b
.
a
并且b
只是您传入的参数calc()
。以前,您可以随意称呼它们,但在内部,calc()
您可以将它们称为a
and b
。
So you can have this:
所以你可以有这个:
int foo = 5;
int bar = 7;
int sum = calc(foo, bar); //12
回答by Thirumalai Parthasarathi
Pre-conditions are the things that must be true before a method is called. The method tells clients "this is what I expect from you".
前提条件是在调用方法之前必须为真的事情。该方法告诉客户“这就是我对你的期望”。
Post-conditions are the things that must be true after the method is complete. The method tells clients "this is what I promise to do for you".
后置条件是方法完成后必须为真的事情。该方法告诉客户“这是我承诺为您做的”。
Invariants are the things that are always true and won't change. The method tells clients "if this was true before you called me, I promise it'll still be true when I'm done".
不变量是永远正确且不会改变的事物。该方法告诉客户“如果在你打电话给我之前这是真的,我保证当我完成后它仍然是真的”。
answer quoted from stackoverflow question
从stackoverflow问题引用的答案
回答by Clark
Your code is in a contract with other bits and pieces of code. The pre-condition is essentially what must be met initially in order for your code to guarantee that it will do what it is supposed to do.
您的代码与其他零碎的代码签订了合同。先决条件本质上是最初必须满足的条件,以便您的代码保证它会做它应该做的事情。
For example, a binary search would have the pre-condition that the thing you are searching through must be sorted.
例如,二进制搜索的前提条件是您正在搜索的内容必须经过排序。
On the other hand, the post-condition is what the code guarantees if the pre-condition is satisfied. For example, in the situation of the binary search, we are guaranteed to find the location of what we were searching for, or return -1 in the case where we don't find anything.
另一方面,后置条件是代码在满足前置条件时保证的内容。例如,在二分查找的情况下,我们保证找到我们要查找的位置,或者在找不到任何东西的情况下返回-1。
The pre-condition is almost like another thing on top of your parameters. They don't usually affect code directly, but it's useful when other people are using your code, so they use it correctly.
前提条件几乎就像是参数之上的另一件事。它们通常不会直接影响代码,但是当其他人使用您的代码时它很有用,因此他们可以正确使用它。
回答by schlingel
A invariant is a combined precondition and postcondition. It has to be valid before and after a call to a method. A precondition has to be fullfilled before a method can be run and a postcondition afterwards.
不变量是前置条件和后置条件的组合。它在调用方法之前和之后都必须有效。必须先满足先决条件,然后才能运行方法,然后才能运行后置条件。
Java has no mechanisms for the condition checking built in but, here's a little example.
Java 没有内置的条件检查机制,但这里有一个小例子。
public class Calc {
private int value = 0;
private boolean isValid() {
return value >= 0;
}
// this method has the validity as invariant. It's true before and after a successful call.
public void add(int val) {
// precondition
if(!isValid()) {
throw new IllegalStateException();
}
// actual "logic"
value += val;
// postcondition
if(!isValid()) {
throw new IllegalStateException();
}
}
}
As you can see the conditions can be violated. In this case you (normally) use exceptions in Java.
如您所见,可以违反条件。在这种情况下,您(通常)在 Java 中使用异常。
回答by Andromeda
private int calc(int a, int b){
return a + b;
//would the parameters be pre-conditions and the return value be a post-condition?
}
Is a function that takes two intand returns an int, which is the summation of aand b.
是一个函数,它接受两个int并返回一个int,它是a和b的总和。
You would normally call the calc function in main as
您通常将 main 中的 calc 函数称为
public static void main(String[] args)
{
int a = 3, b = 4;
int sum = calc(a, b);
}
when you do that, a copy of aand bis passed to calcbut the original values of aand bare not affected by the calcfunction as parameters are passed by valuein Java.
当你这样做时,a和b的副本被传递给calc但a和b的原始值不受calc函数的影响,因为参数在Java 中是按值传递的。
回答by babernathy
A precondition is something that has to be true about the parameters that a function takes. So it isn't enough to say what the variables are, but you need to say something about their nature. For example, a and b must be integers. A post condition states what must be true after the function completes. In your example, it would be the fact that your function must produce the sum of a and b. The precondition and post condition can actually result in two methods, especially in a language like Java. What if you had a precondition that stated simply "The two parameters must be numerical". Then you would have to account for not only integers, but floating points.
前提条件是关于函数所采用的参数必须为真。因此,仅仅说明变量是什么是不够的,但您需要说明它们的性质。例如,a 和 b 必须是整数。后置条件说明函数完成后必须为真。在您的示例中,事实是您的函数必须产生 a 和 b 的总和。前置条件和后置条件实际上可以产生两种方法,尤其是在像 Java 这样的语言中。如果您有一个简单说明“两个参数必须是数字的”的先决条件,该怎么办?那么你不仅要考虑整数,还要考虑浮点数。
Hope that helps.
希望有帮助。