Thinking in java 筆記(一)

來源:互聯網
上載者:User

public available to eveyone

private 只可以在該類內部引用

protected 繼承類可以訪問

default 同一個package中可以訪問

Composition是將其他類的對象作為自己的成員。設計時,首先考慮使用composition而不是inheritance,因為composition更靈活,而且inheritance會使設計複雜化。

當一個子類對象的引用被賦給一個父類引用變數時,只能訪問父類定義的對象的那一部分。

LSP(substitution principle) ,要求子類的方法存取權限不小於基類的方法的存取權限。在進行設計的時候,我們盡量從抽象類別繼承,而不是從具體類繼承。

裡氏代換原則是對開閉原則的補充,它講的是基類和子類的關係。只有當這種關係存在時,裡氏代換關係才存在。

"正方形是長方形"是一個理解裡氏代換原則的最經典的例子。在數學領域裡,正方形毫無疑問是長方形,它是一個長寬相等的長方形。所以,應該讓正方形繼承自長方形。

長方形類如程式1所示。

程式1  長方形類Rectangle.java

package principle.liskovsubstitution;  

public class Rectangle {  

private int height;  

private int width;  

public int getHeight() {

return height; 

}

public void setHeight(int height) {

this.height = height; 

}

public int getWidth() {

return width; 

} 

public void setWidth(int width) { 

this.width = width; 

} 

} 

繼承了長方形的正方形類如程式2所示。

程式2  正方形類Square.java

package principle.liskovsubstitution;

public class Square extends Rectangle {

public void setWidth(int width) {

super.setWidth(width);  

super.setHeight(width);  

}

public void setHeight(int height) {

super.setWidth(height); 

super.setHeight(height);

} 

} 

由於正方形的長度和寬度必須相等,所以在方法setLength()和setWidth()中,對長度和寬度賦值相同。

程式3所示的測試類別中的函數zoom()用來增加長方形的長和寬。

程式3  測試類別TestRectangle.java

package principle.liskovsubstitution; 

public class TestRectangle {

public void zoom(Rectangle rectangle, int width, int height){ 

rectangle.setWidth(rectangle.getWidth() + width); 

rectangle.setHeight(rectangle.getHeight() + height); 

}

} 

顯然,當增加的長度和寬度不同時,不能夠將其中的長方形換成其子類正方形。這就違反了裡氏代換原則。

為了符合裡氏代換原則,我們可以為長方形和正方形建立一個父類Base,並在其中定義好共有的屬性,並定義一個zoom()抽象函數,

如程式4所示。

程式4  父類Base.java

package principle.liskovsubstitution;

public abstract class Base {

private int height; 

private int width; 

public int getHeight() {

return height;

}

public void setHeight(int height) {

this.height = height;

} 

public int getWidth() {

return width; 

}

public void setWidth(int width) {

this.width = width; 

} 

public abstract void zoom(int width, int height); 

} 

長方形類繼承自該父類,並編寫自己的zoom()實現函數,如程式5所示。

程式5  修改後的長方形類BaseRectangle.java

package principle.liskovsubstitution;

public class BaseRectangle extends Base {

public void zoom(int width, int height) {

setWidth(getWidth() + width);

setHeight(getHeight() + height);

}  

} 

正方形類也繼承自該父類,並編寫自己的zoom()實現函數,如程式6所示。

程式6  修改後的正方形類BaseSquare.java

package principle.liskovsubstitution;

public class BaseSquare extends Base {

public void setWidth(int width) {

super.setWidth(width);

super.setHeight(width);

}

public void setHeight(int height) {

super.setWidth(height);

super.setHeight(height);

}

public void zoom(int width, int height) {

int length = (width + height) /2;

setWidth(getWidth() + length);

setHeight(getHeight() + length);

}

} 

編寫測試函數如程式7所示。

程式7  修改後的測試類別BastTest.java

package principle.liskovsubstitution;

public class BastTest {

public void zoom(Base base, int width, int height) {

base.zoom(width, height);

}

}

此時的Base類可以被它的子類Rectangle和Square所替代,而不用改變測試代碼。這就是符合裡氏代換原則的編寫方式。

設計模式的六大原則:

開閉原則對擴充開放,對修改關閉。

裡氏代換原則:基類可以被子類替換。

合成複用原則:要依賴於抽象,不要依賴於具體。

介面隔離原則:使用多個隔離的介面,比使用單個介面好。

迪米特法則:一個軟體實體應當儘可能少地與其他實體發生相互作用。

依賴倒轉原則:盡量使用合成/彙總,而不是使用繼承。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.