如何在Java类方法或构造函数中插入前提条件? [英] How do I insert a precondition in a java class method or constructor?

查看:340
本文介绍了如何在Java类方法或构造函数中插入前提条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在学习的Java类.该书提到了前置条件和后置条件,但没有提供任何示例如何编码它们.继续讨论断言,我对此很失望,但是我正在做的作业特别声明要插入前提条件并使用断言测试前提条件.

This is for a java class I'm taking. The book mentions preconditions and postconditions but doesn't give any examples how to code them. It goes on to talk about asserts, I have that down, but the assignment I'm doing specifically states to insert preconditions and test the preconditions with asserts.

任何帮助都会很棒.

推荐答案

像Eiffel这样的语言将前置条件"和后置条件"作为该语言的基本组成部分.

Languages like Eiffel support "preconditions" and "postconditions" as a basic part of the language.

一个令人信服的论点是,对象构造函数"的整个目的是精确地 建立类不变式".

One can make a compelling argument that the whole purpose of an "object constructor" is precisely to establish "the class invariant".

但是对于Java(几乎与其他所有后C ++面向对象语言一样),您几乎必须伪造它.

But with Java (as with just about every other post-C++ object oriented language), you pretty much have to fake it.

这是有关利用Java断言"的出色技术说明:

Here's an excellent tech note on leveraging Java "assert":

这篇关于如何在Java类方法或构造函数中插入前提条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆