乳腺结节是什么引起的| 入宅是什么意思| 色调是什么意思| 青柠是什么水果| 蘑菇是什么菌| 过敏吃什么药最有效| 补脾吃什么好| 刘强东属什么生肖| 66年出生属什么生肖| 脊椎痛什么原因| 梦见孩子结婚什么预兆| 康复治疗是做什么的| 今年52岁属什么生肖| 美洲大蠊主治什么病| 总是感觉有尿意是什么原因| 性生活过多有什么危害| 舌头肿了是什么原因| 芦笋是什么植物| 禾字五行属什么的| 角化型脚气用什么药膏| 小雪时节吃什么| 保家仙是什么| 冷鲜肉和新鲜肉有什么区别| 乳腺实性结节是什么意思| 09年的牛是什么命| 脑白质疏松是什么意思| 6.21什么星座| 腹泻便溏是什么意思| mw是什么单位| 三叉神经痛吃什么药效果好| 肌肉萎缩挂什么科| 四级专家是什么级别| ef是什么意思| 一个金字旁一个各念什么| 锌过量会引发什么症状| classic是什么意思| k值是什么意思| 什么是虚无主义| 大便暗红色是什么原因| 条子是什么意思| 奇异果是什么水果| 杵状指见于什么疾病| 今天穿什么| 支原体弱阳性是什么意思| 蜘蛛结网预示着什么| 梦见生姜是什么意思| 浮想联翩是什么意思| 烀是什么意思| 梦见被狗咬是什么预兆| 教师节应该送老师什么花| 滇红属于什么茶| 退化是什么意思| 峰会什么时候开| 低压高是什么原因造成的| 澍在人名中读什么| 小孩打喷嚏流鼻涕吃什么药| 铁蛋白高吃什么食物好| 1924年属什么生肖| 升结肠管状腺瘤是什么意思| 肚脐眼左右两边疼是什么原因| 摆地摊卖什么最赚钱而且很受欢迎| 白花花的什么| 医院属于什么单位| 自控能力是什么意思| 什么是肌无力| 血糖高是什么意思| 禄代表什么生肖| 先父遗传是什么意思| 天意是什么意思| 寒气和湿气有什么区别| 牙龈痛什么原因| 中性粒细胞偏低是什么意思| 痰核是什么意思| 受控是什么意思| 凤辇是什么意思| 负荆请罪的负是什么意思| 鲨鱼是什么动物| 光斑是什么意思| 吃中药不可以吃什么水果| 孕妇忌吃什么| 肾囊肿是什么原因引起的| 怀疑肝不好挂什么科室| 1978年属什么生肖| 甲减是什么意思| VH是什么品牌| 晚上睡不着觉吃什么药| 釜底抽薪是什么意思| 脑梗什么原因导致的| 朔字五行属什么| 33数字代表什么意思| 摩羯座男生喜欢什么样的女生| 勃艮第红是什么颜色| 红枣什么时候吃最好| 9月19号什么星座| 宫腔积液是什么意思| 乳房疼挂什么科室| 月经期血块多是什么原因| 樱桃有什么功效| 得艾滋病有什么症状| 梨涡是什么意思| 火腿肠炒什么好吃| 慢性结肠炎吃什么药| 拐枣泡酒有什么功效| 吃葡萄对身体有什么好处| 大暑是什么时候| 感冒鼻塞吃什么药| 九月十九是什么星座| 酒后吃什么水果好| 肠结核是什么病| vivo是什么牌子| 小孩流鼻血挂什么科| 骨密度高是什么意思| 中医师承是什么意思| 浮想联翩什么意思| 什么妖魔鬼怪什么美女画皮| 鬼长什么样子| 晚上难以入睡是什么原因| 高血糖能吃什么水果| 痒是什么原因引起的| 湿气重不能吃什么| 产酸克雷伯菌属于什么菌| 午未合化什么| 出痧是什么原因| 转氨酶高是什么病| 梦到捡金子首饰是什么意思| 三里屯有什么好玩的地方| 碱性磷酸酶高是什么意思| 40周年是什么婚| 新生儿老打嗝什么原因| 什么叫入伏| 西班牙用的是什么货币| 去医院查怀孕挂什么科| 梦见自己的哥哥死了是什么意思| 毛主席什么时候去世的| 公鸡为什么会打鸣| 豆浆和豆奶有什么区别| 什么是性质| 萎缩性胃炎吃什么好| 喝什么饮料解酒最快最有效| 梦到甘蔗代表什么预兆| 舒字属于五行属什么| 肠炎吃什么药好的快| 做梦梦见鬼是什么预兆| 精神紊乱吃什么药| 头晕吃什么可以缓解| 腹泻吃什么食物好得快| 土阜念什么| 性转是什么意思| 上焦有火吃什么中成药| 他达拉非是什么药| 急性腮腺炎吃什么药| 参考是什么意思| 10月30号什么星座| 考试前吃什么好| 蔓越莓是什么水果| 过敏性鼻炎不能吃什么| 脐带绕颈有什么症状| icloud是什么| 十月十六号是什么星座| 阴离子是什么| 单活胎是什么意思| cps是什么意思啊| 梦到离婚是什么征兆| 翻车了是什么意思| 裙摆是什么| 唱腔是什么意思| 大运是什么意思| 公分是什么意思| 渗析是什么意思| 阳光照耀是什么意思| 尿培养是检查什么病| 亦或是什么意思| 先自度其足的度是什么意思| 子宁不嗣音什么意思| 仰面朝天是什么生肖| 怀孕不能吃什么水果| 穿山甲吃什么| 脾胃不好吃什么调理| 咖啡伴侣是什么| 反酸烧心吃什么药效果好| 教皇是什么意思| 性格好的女生是什么样| 白色t恤配什么裤子| 人参片泡水喝有什么功效和作用| 小气是什么意思| 溦是什么意思| 农历9月17日是什么星座| 什么人不宜吃海参| 每天喝牛奶有什么好处| 粉蒸肉用什么肉好吃| 柏拉图式是什么意思| 什么是宦官| 柴火饭是什么意思| 秀五行属什么| 头晕为什么做眼震检查| 洛什么意思| 2月份生日是什么星座| 复印病历需要什么证件| 为什么过敏反复发作| 绝望的绝是什么意思| 螨虫是什么| 天蝎座和什么星座配| 米氮平是什么药| 鱼泡是什么| 什么水果最甜| 一什么波纹| 晚上喝蜂蜜水有什么好处| 流星雨是什么意思| 蓓字五行属什么| 肥皂剧是什么意思| 提前吃什么喝酒不醉| 属兔本命佛是什么佛| 谷丙转氨酶偏低是什么意思| 无创是什么意思| 慢性胆囊炎吃什么药| 当归和党参有什么区别| 肝左叶囊肿是什么意思| 皮肤经常痒是什么原因| 2月25日是什么星座| 清真是什么意思| 酱瓜是什么瓜| 去鱼腥味最好的方法是什么| 什么规律| 睡不着吃什么药最有效| 女人为什么会得甲状腺| 口干舌燥吃什么中成药| 五花八门是什么意思| 1月1日是什么星座| 13岁属什么生肖| 腰酸是什么原因女性| 伪骨科是什么| 血压高压低是什么原因| 咖啡什么时候喝最好| 背痒是什么原因| 夏天脚出汗是什么原因| 命是什么意思| 白芷是什么| 为什么医院不建议药流| 下架是什么意思| 老人怕冷是什么原因| 尿路感染用什么药好| 乙肝大三阳是什么意思| 舌头痒是什么原因| 煤油对人体有什么危害| 肝火旺吃什么调理| 白带有点黄是什么原因| lemaire是什么品牌| 草字头弓读什么字| 皮疹长什么样| 起大运是什么意思| 五月十三号是什么日子| 梦见自己掉头发是什么意思| icu和ccu有什么区别| 西楚霸王是什么生肖| 子宫肌腺症是什么病| 三个六代表什么意思| 养猫的人容易得什么病| 吃什么鱼最健康| 毕业送什么花| 洋葱什么时候种| 云彩像什么| 夏天什么时候结束| 哇咔咔是什么意思| 生动是什么意思| 百度Jump to content

国家信访局召开新闻通气会解读《信访工作责任制实施办法》

From Wikipedia, the free encyclopedia
百度 要注意用好用活丰富的党史资源,使之成为激励人民不断开拓前进的强大精神力量”。

In computer programming, specifically object-oriented programming, a class invariant (or type invariant) is an invariant used for constraining objects of a class. Methods of the class should preserve the invariant. The class invariant constrains the state stored in the object.

Class invariants are established during construction and constantly maintained between calls to public methods. Code within functions may break invariants as long as the invariants are restored before a public function ends. With concurrency, maintaining the invariant in methods typically requires a critical section to be established by locking the state using a mutex.

An object invariant, or representation invariant, is a computer programming construct consisting of a set of invariant properties that remain uncompromised regardless of the state of the object. This ensures that the object will always meet predefined conditions, and that methods may, therefore, always reference the object without the risk of making inaccurate presumptions. Defining class invariants can help programmers and testers to catch more bugs during software testing.

Class invariants and inheritance

[edit]

The useful effect of class invariants in object-oriented software is enhanced in the presence of inheritance. Class invariants are inherited, that is, "the invariants of all the parents of a class apply to the class itself."[1]

Inheritance can allow descendant classes to alter implementation data of parent classes, so it would be possible for a descendant class to change the state of instances in a way that made them invalid from the viewpoint of the parent class. The concern for this type of misbehaving descendant is one reason object-oriented software designers give for favoring composition over inheritance (i.e., inheritance breaks encapsulation).[2]

However, because class invariants are inherited, the class invariant for any particular class consists of any invariant assertions coded immediately on that class in conjunction with all the invariant clauses inherited from the class's parents. This means that even though descendant classes may have access to the implementation data of their parents, the class invariant can prevent them from manipulating those data in any way that produces an invalid instance at runtime.

Programming language support

[edit]

Assertions

[edit]

Common programming languages like Python,[3] PHP,[4] JavaScript,[citation needed] C++ and Java support assertions by default, which can be used to define class invariants. A common pattern to implement invariants in classes is for the constructor of the class to throw an exception if the invariant is not satisfied. For mutable classes, e.g., in the presence of setter methods or when they depend on other mutable objects, one might in addition want to check after each modification whether the invariant was correctly re-established, which guarantees that method implementations indeed preserve the class invariants.

Native support

[edit]

The class invariant is an essential component of design by contract. So, programming languages that provide full native support for design by contract, such as Eiffel, Ada, Dafny, and D, will also provide full support for class invariants.

Non-native support

[edit]

For C++, the Loki Library provides a framework for checking class invariants, static data invariants, and exception safety.

For Java, there is a more powerful tool called Java Modeling Language that provides a more robust way of defining class invariants.

Examples

[edit]

Native support

[edit]

Ada

[edit]

The Ada programming language has native support for type invariants (as well as pre- and postconditions, subtype predicates, etc.). A type invariant may be given on a private type (for example to define a relationship between its abstract properties), or on its full definition (typically to help in verifying the correctness of the implementation of the type).[5] Here is an example of a type invariant given on the full definition of a private type used to represent a logical stack. The implementation uses an array, and the type invariant specifies certain properties of the implementation that enable proofs of safety. In this case, the invariant ensures that, for a stack of logical depth N, the first N elements of the array are valid values. The Default_Initial_Condition of the Stack type, by specifying an empty stack, ensures the initial truth of the invariant, and Push preserves the invariant. The truth of the invariant then enables Pop to rely on the fact that the top of the stack is a valid value, which is needed to prove Pop's postcondition. A more complex type invariant would enable the proof of full functional correctness, such as that Pop returns the value passed into a corresponding Push, but in this case we are merely trying to prove that Pop does not return an Invalid_Value.

generic
    type Item is private;
    Invalid_Value : in Item;
package Stacks is
    type Stack(Max_Depth : Positive) is private
        with Default_Initial_Condition => Is_Empty (Stack);

    function Is_Empty(S : in Stack) return Boolean;
    function Is_Full(S : in Stack) return Boolean;

    procedure Push(S : in out Stack; I : in Item)
        with Pre  => not Is_Full(S) and then I /= Invalid_Value,
             Post => not Is_Empty(S);
    procedure Pop(S : in out Stack; I : out Item)
        with Pre  => not Is_Empty(S),
             Post => not Is_Full(S) and then I /= Invalid_Value;
private
    type Item_Array is array (Positive range <>) of Item;

    type Stack(Max_Depth : Positive) is record
        Length : Natural := 0;
        Data : Item_Array (1 .. Max_Depth) := (others => Invalid_Value);
    end record
        with Type_Invariant => Length <= Max_Depth and then
          (for all J in 1 .. Length => Data (J) /= Invalid_Value);

    function Is_Empty(S : in Stack) return Boolean
        is (S.Length = 0);
    function Is_Full(S : in Stack) return Boolean
        is (S.Length = S.Max_Depth);
end Stacks;

D

[edit]

D programming language has native support of class invariants, as well as other contract programming techniques. Here is an example from the official documentation.[6]

class Date {
  int day;
  int hour;

  invariant() {
    assert(day >= 1 && day <= 31);
    assert(hour >= 0 && hour <= 23);
  }
}

Eiffel

[edit]

In Eiffel, the class invariant appears at the end of the class following the keyword invariant.

class
	DATE

create
	make

feature {NONE} -- Initialization

	make (a_day: INTEGER; a_hour: INTEGER)
			-- Initialize `Current' with `a_day' and `a_hour'.
		require
			valid_day: a_day >= 1 and a_day <= 31
			valid_hour: a_hour >= 0 and a_hour <= 23
		do
			day := a_day
			hour := a_hour
		ensure
			day_set: day = a_day
			hour_set: hour = a_hour
		end

feature -- Access

	day: INTEGER
		-- Day of month for `Current'

	hour: INTEGER
		-- Hour of day for `Current'

feature -- Element change

	set_day (a_day: INTEGER)
			-- Set `day' to `a_day'
		require
			valid_argument: a_day >= 1 and a_day <= 31
		do
			day := a_day
		ensure
			day_set: day = a_day
		end

	set_hour (a_hour: INTEGER)
			-- Set `hour' to `a_hour'
		require
			valid_argument: a_hour >= 0 and a_hour <= 23
		do
			hour := a_hour
		ensure
			hour_set: hour = a_hour
		end

invariant
	valid_day: day >= 1 and day <= 31
	valid_hour: hour >= 0 and hour <= 23
end

Non-native support

[edit]

C++

[edit]

The Loki (C++) library provides a framework written by Richard Sposato for checking class invariants, static data invariants, and exception safety level.

This is an example of how a class can use Loki::Checker to verify that invariants remain true after an object changes. The example uses a geopoint object to store a location on Earth as a coordinate of latitude and longitude.

The geopoint invariants are:

  • latitude may not be more than 90° north.
  • latitude may not be less than -90° south.
  • longitude may not be more than 180° east.
  • longitude may not be less than -180° west.
#include <loki/Checker.h>  // Needed to check class invariants.

#include <Degrees.hpp>

class GeoPoint {
 public:
  GeoPoint(Degrees latitude, Degrees longitude);

  /// Move function will move location of GeoPoint.
  void Move(Degrees latitude_change, Degrees longitude_change) {
    // The checker object calls IsValid at function entry and exit to prove this
    // GeoPoint object is valid. The checker also guarantees GeoPoint::Move
    // function will never throw.
    CheckFor::CheckForNoThrow checker(this, &IsValid);

    latitude_ += latitude_change;
    if (latitude_ >= 90.0) latitude_ = 90.0;
    if (latitude_ <= -90.0) latitude_ = -90.0;

    longitude_ += longitude_change;
    while (longitude_ >= 180.0) longitude_ -= 360.0;
    while (longitude_ <= -180.0) longitude_ += 360.0;
  }

 private:
  /** @note CheckFor performs validity checking in many functions to determine
   if the code violated any invariants, if any content changed, or if the
   function threw an exception.
   */
  using CheckFor = ::Loki::CheckFor<const GeoPoint>;

  /// This function checks all object invariants.
  bool IsValid() const {
    assert(this != nullptr);
    assert(latitude_ >= -90.0);
    assert(latitude_ <= 90.0);
    assert(longitude_ >= -180.0);
    assert(longitude_ <= 180.0);
    return true;
  }

  Degrees latitude_;   ///< Degrees from equator. Positive is north, negative is
                       ///< south.
  Degrees longitude_;  ///< Degrees from Prime Meridian. Positive is east,
                       ///< negative is west.
}

Java

[edit]

This is an example of a class invariant in the Java programming language with Java Modeling Language. The invariant must hold to be true after the constructor is finished and at the entry and exit of all public member functions. Public member functions should define precondition and postcondition to help ensure the class invariant.

public class Date {
    int /*@spec_public@*/ day;
    int /*@spec_public@*/ hour;

    /*@invariant day >= 1 && day <= 31; @*/ //class invariant
    /*@invariant hour >= 0 && hour <= 23; @*/ //class invariant

    /*@
    @requires d >= 1 && d <= 31;
    @requires h >= 0 && h <= 23;
    @*/
    public Date(int d, int h) { // constructor
        day = d;
        hour = h;
    }

    /*@
    @requires d >= 1 && d <= 31;
    @ensures day == d;
    @*/
    public void setDay(int d) {
        day = d;
    }

    /*@
    @requires h >= 0 && h <= 23;
    @ensures hour == h;
    @*/
    public void setHour(int h) {
        hour = h;
    }
}

References

[edit]
  1. ^ Meyer, Bertrand. Object-Oriented Software Construction, second edition, Prentice Hall, 1997, p. 570.
  2. ^ E. Gamma, R. Helm, R. Johnson, and J. Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, Reading, Massachusetts, 1995., p. 20.
  3. ^ Official Python Docs, assert statement
  4. ^ "PHP assert function". Archived from the original on 2025-08-14.
  5. ^ "Ada Reference Manual 7.3.2 Type Invariants". ada-auth.org. Retrieved 2025-08-14.
  6. ^ "Contract Programming - D Programming Language". dlang.org. Retrieved 2025-08-14.
尿赤是什么意思 什么叫做基本工资 痱子粉和爽身粉有什么区别 心率130左右意味着什么 精干是什么意思
忠于自己是什么意思 红烧肉是什么肉 教唆什么意思 一九七七年属什么生肖 衣带渐宽终不悔是什么意思
来月经期间吃什么最好 大牙什么时候换 梦见过年是什么意思 你的书包里有什么英文 菱形脸适合什么刘海
天台是什么意思 女生下面什么样 右手手指头麻木是什么病的前兆 双肺多发结节是什么意思 桑黄是什么树上长出来的
梅毒抗体阳性说明什么hcv7jop9ns1r.cn 小满是什么意思hcv8jop6ns0r.cn 电饭锅内胆是什么材质hcv9jop3ns4r.cn 社保卡属于什么银行hcv9jop7ns5r.cn 耳朵里面痒用什么药hcv9jop0ns6r.cn
耳结是什么原因造成的hcv8jop4ns6r.cn 妇科检查bv阳性是什么意思hcv9jop6ns2r.cn 田螺小子是什么意思xinmaowt.com 迪丽热巴的全名叫什么hcv8jop2ns8r.cn 甲亢病是什么原因引起的hcv8jop3ns5r.cn
重睑术是什么意思hcv9jop1ns8r.cn 金牛女喜欢什么样的男生hcv9jop1ns6r.cn 8月12号是什么星座hcv9jop3ns6r.cn 什么样的升旗仪式xscnpatent.com 一个月一个泉是什么字kuyehao.com
衤叫什么偏旁hcv8jop0ns7r.cn 什么药护肝效果最好hcv9jop5ns9r.cn 刻舟求剑的寓意是什么hcv7jop6ns9r.cn 兄弟是什么生肖hcv7jop7ns2r.cn 什么叫糖类抗原hcv7jop7ns0r.cn
百度