里氏替換原則
在物件導向的程式設計中,里氏替換原則(Liskov Substitution principle)是對子類型的特別定義。它由芭芭拉·利斯科夫(Barbara Liskov)在1987年在一次會議上名為「資料的抽象與層次」的演說中首先提出。[1]
![Portrait of Barbara Liskov](http://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Barbara_Liskov_MIT_computer_scientist_2010.jpg/220px-Barbara_Liskov_MIT_computer_scientist_2010.jpg)
里氏替換原則的內容可以描述為: 「衍生類別(子類)對象可以在程式中代替其基礎類別(超類)對象。」 以上內容並非利斯科夫的原文,而是譯自羅伯特·馬丁(Robert Martin)對原文的解讀。其原文為:
- Let be a property provable about objects of type . Then should be true for objects of type where is a subtype of .
參照
編輯- ^ Liskov, Barbara. Data abstraction and hierarchy. 1987-10-04 [2008-03-23]. (原始內容存檔於2019-06-30).
參考文獻
編輯一般參考
- Leavens, Gary T.; Dhara, Krishna K. Concepts of Behavioral Subtyping and a Sketch of Their Extension to Component-Bases Systems. Leavens, Gary T.; Sitaraman, Murali (編). Foundations of component-based systems. Cambridge University Press. 2000. ISBN 0-521-77164-1. This paper surveys various notions of behavioral subtyping, including Liskov and Wing's.
- Liskov, B. H.; Wing, J. M. A behavioral notion of subtyping. ACM Trans. Program. Lang. Syst. 16 (6). November 1994: 1811–41. doi:10.1145/197320.197383. 參數
|journal=
與模板{{cite conference}}
不匹配(建議改用{{cite journal}}
或|book-title=
) (幫助)
An updated version appeared: Liskov, Barbara; Wing, Jeannette. Behavioral Subtyping Using Invariants and Constraints (技術報告). Carnegie Mellon University. July 1999 [2022-01-06]. CMU-CS-99-156. (原始內容存檔於2012-07-16). The formalization of the principle by its authors. - Plösch, Reinhold. Contracts, scenarios and prototypes: an integrated approach to high quality software. Springer. 2004. ISBN 3-540-43486-0. Contains a gentler introduction to behavioral subtyping in its various forms in chapter 2.
- Martin, Robert C. The Liskov Substitution Principle (PDF). C++ Report. March 1996. (原始內容 (PDF)存檔於2015-11-28). An article popular in the object-oriented programming community that gives several examples of LSP violations.
- Majorinc, Kazimir. Ellipse-Circle Dilemma and Inverse Inheritance. ITI 98, Proceedings of the 20th International Conference of Information Technology Interfaces, Pula, 1998. : 627–632. ISSN 1330-1012. OCLC 894960131.
|journal=
被忽略 (幫助) This paper discusses LSP in the mentioned context.
具體參考
- Liskov, B. Keynote address — data abstraction and hierarchy. ACM SIGPLAN Notices. May 1988, 23 (5): 17–34. doi:10.1145/62139.62141. A keynote address in which Liskov first formulated the principle.
- Meyer, B. [[Object-oriented Software Construction|物件導向軟體構建]]. Prentice Hall. 1988. ISBN 0-13-629031-0.
參閲
編輯- SOLID - 「SOLID」中的 L 指代了里氏替換原則
- 類型簽名
- 參考透明度(Referential transparency)
- 求精法 (編程計算)(Refinement (computing))
- 組合優於繼承(Composition over inheritance)
外部連結
編輯- Norvell, T.S. The Liskov Substitution Principle (PDF). 2003 [2022-01-06]. (原始內容 (PDF)存檔於2022-01-30).
- Liskov Substitution Principle Explained (頁面存檔備份,存於網際網路檔案館)
- SOLID Class Design: The Liskov Substitution Principle (頁面存檔備份,存於網際網路檔案館)
- LSP: Liskov Substitution Principle (頁面存檔備份,存於網際網路檔案館)