I googled the topic, but besides Wikipedia I didn't find any further useful documentation or articles.
我搜索了这个主题,但除了维基百科,我没有找到任何进一步有用的文档或文章。
Can anybody explain to me in simple words what it means or refer me to some nice and easy to understand documentation?
任何人都可以用简单的语言向我解释它的含义或者向我推荐一些简单易懂的文档吗?
67
It doesn't mean anything in particular in reference to java.
它并不意味着特别指向java。
A class invariant is simply a property that holds for all instances of a class, always, no matter what other code does.
类不变量只是一个属性,它始终保存类的所有实例,无论其他代码是什么。
For example,
例如,
class X {
final Y y = new Y();
}
X has the class invariant that there is a y
property and it is never null
and it has a value of type Y
.
X具有类不变量,即存在y属性,它永远不为null,并且其值为Y.
class Counter {
private int x;
public int count() { return x++; }
}
fails to maintain two important invariants
无法保持两个重要的不变量
count
never returns a negative value because of possible underflow.count
are strictly monotonically increasing.The modified class preserves those two invariants.
修改后的类保留了这两个不变量。
class Counter {
private int x;
public synchronized int count() {
if (x == Integer.MAX_VALUE) { throw new IllegalStateException(); }
return x++;
}
}
but fails to preserve the invariant that calls to count
always succeed normally (absent TCB-violations†) because count
might throw an exception or it might block if a deadlocked thread owns the counter's monitor.
但是无法保留计数调用总是正常成功(不存在TCB违规†)的不变量,因为count可能会抛出异常,或者如果死锁线程拥有计数器的监视器,它可能会阻塞。
Each language with classes make it easy to maintain some class invariants but not others. Java is no exception:
每个带有类的语言都可以很容易地维护一些类不变量而不是其他类。 Java也不例外:
private
fields, so invariants that rely on private data are easy to maintain.null
values to sneak in in many ways, so it is tough to maintain "has a real value" invariants.† - An externality or TCB violation is an event which a systems designer optimistically assumes will not happen.
† - 外部性或TCB违规是系统设计者乐观地认为不会发生的事件。
Typically we just trust that the basic hardware works as advertised when talking about properties of high-level languages built on them, and our arguments that invariants hold don't take into account the possibility of:
通常我们只相信基本硬件在讨论构建在它们上面的高级语言的属性时就像宣传的那样工作,并且不变量持有的参数没有考虑到以下可能性:
setAccessible
to modify private
lookup tables.For some systems our TCB might include only parts of the system, so we might not assume that
对于某些系统,我们的TCB可能只包含系统的一部分,因此我们可能不会这样认为
but we might assume that
但我们可能会认为
The higher-level a system, the larger its TCB typically is, but the more unreliable things you can get out of your TCB, the more likely your invariants are to hold, and the more reliable your system will be in the long run.
系统级别越高,其TCB通常越大,但是从TCB中获得的事物越不可靠,您的不变量就越有可能保持不变,从长远来看系统的可靠性就越高。
9
They are facts that must be true about an instance class. For example if a class has a property X and invariant can be X must be greater then 0. To my knowledge there is no built-in method for maintaining invariants you must make properties private and make sure your getters and setters enforce invariance property.
它们是关于实例类必须是真实的事实。例如,如果一个类具有属性X且不变,则X必须大于0.据我所知,没有用于维护不变量的内置方法,必须使属性为私有,并确保getter和setter强制执行不变属性。
There are annotations available which can check properties using reflection and interceptors. http://docs.oracle.com/javaee/7/api/javax/validation/constraints/package-summary.html
有一些注释可以使用反射和拦截器检查属性。 http://docs.oracle.com/javaee/7/api/javax/validation/constraints/package-summary.html
8
Invariant means something that should stick to its conditions no matter whatever changes or whoever uses/transforms it. That is to say, a property of a class always fulfills or satisfies some condition even after going through transformations by using public methods. So, the client or user of this class is ensured about the class and its property.
不变意味着一些东西应该坚持其条件,无论任何变化或任何使用/转换它的人。也就是说,即使在通过使用公共方法进行转换之后,类的属性总是满足或满足某些条件。因此,确保该类及其属性的此类的客户端或用户。
For example,
例如,
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2012/01/17/24b91011e855da44e64f03cfb567b77c.html。