第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

怎樣設(shè)計(jì)Rectangle和Square類(lèi)才能使之滿足OOP的Liskov substitution原則

怎樣設(shè)計(jì)Rectangle和Square類(lèi)才能使之滿足OOP的Liskov substitution原則

明月笑刀無(wú)情 2019-04-06 08:31:57
Liskovsubstitution(里氏替換)是oop五原則(SOLID)之一,wikipedia上的介紹如下Substitutabilityisaprincipleinobject-orientedprogramming.Itstatesthat,inacomputerprogram,ifSisasubtypeofT,thenobjectsoftypeTmaybereplacedwithobjectsoftypeS(i.e.,objectsoftypeSmaybesubstitutedforobjectsoftypeT)withoutalteringanyofthedesirablepropertiesofthatprogram(correctness,taskperformed,etc.).其中有提到一個(gè)比較經(jīng)典的違反此原則的案例,那就是square和rectangle,先看代碼:publicclassRectangle{privatedoublewidth;privatedoubleheight;publicvoidsetWidth(doublewidth){this.width=width;}publicvoidsetHeight(doubleheight){this.height=height;}publicdoublegetHeight(){returnthis.height;}publicdoublegetWidth(){returnthis.width;}publicdoublegetPerimeter(){return2*width+2*height;}publicdoublegetArea(){returnwidth*height;}}publicclassSquareextendsRectangle{publicvoidsetWidth(doublewidth){this.width=width;this.height=width;}publicvoidsetHeight(doubleheight){this.height=height;this.width=height;}}按照自然規(guī)則,square是特殊的rectangle(width==height),但是在用兩個(gè)class來(lái)表述時(shí)是不符合里氏替換規(guī)則的,因?yàn)閟quare的setter不滿足里氏替換規(guī)則里的Postconditionscannotbeweakenedinasubtype。那么到底怎樣設(shè)計(jì)才行呢?
查看完整描述

2 回答

  • 2 回答
  • 0 關(guān)注
  • 416 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)