use*_*101 2 c# post-conditions
我最近的任务要求我遵守以下条件
“所有方法都有显式的后置条件,而那些具有参数前置条件的方法”
我已经阅读了一些网页,试图解释前后条件,但似乎可以理解它们,有人可以向我解释它们的含义,用法以及如何编写它们吗?
谢谢
(顺便说一句,我正在学习的语言是C#)
在输入方法之前,先决条件必须为true,否则合同无效。退出方法后,后编码应为true。抱歉,我不了解C#,但是如果您知道Java,那么这个选择排序示例可能会有所帮助。例:
public static void selSort(int[] a, int b) {
//Pre-condition: array a is not null and size of unsorted section is bigger than 1.
for(int unsortSz = b; unsortSz >1; unsortSz--) {
int max = 0;
for (int p = 1; p < unsortSz; p++){
if (a[p] > a[max]){
max = p;
}
}
//Post-condition: max is the position of largest element in unsorted part.
// now just swap the last element in unsorted part with max
temp = a[unsortSz-1];
a[unsortSz] = a[max];
a[max] = temp;
}
}
Run Code Online (Sandbox Code Playgroud)