1 / 7

More Inheritance and LSP

More Inheritance and LSP. CS340100, NTHU Yoshi. More about Inheritance. Reuse ? Q1: 你有沒有程式 ” 砍掉重練 ” 的經驗 ? Q2: 你有沒有 ” 再造輪子 ” 的經驗 ? class Rectangle Firstly, we only have this design, and the system was developed class Square

vianca
Download Presentation

More Inheritance and LSP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More Inheritance and LSP CS340100, NTHU Yoshi

  2. More about Inheritance • Reuse? • Q1: 你有沒有程式”砍掉重練”的經驗? • Q2: 你有沒有”再造輪子”的經驗? • class Rectangle • Firstly, we only have this design, and the system was developed • class Square • Then, we have the requirement of Square, and we wrote a class Square • Square “IS A” kind of rectangle

  3. Class Rectangle class Rectangle {double width;double height;publicdoublegetHeight() {return height;    }publicvoidsetHeight(double height) {this.height = height;    }publicdoublegetWidth() {return width;    }publicvoidsetWidth(double width) {this.width = width;    }   }

  4. Class Square class Square extends Rectangle {publicvoidsetHeight(double height) {super.setHeight(height);super.setWidth(height);    }publicvoidsetWidth(double width) {super.setHeight(width);super.setWidth(width);    }}

  5. For Instance… void g(Rectangle r) {r.setWidth(5);r.setHeight(4);    if (r.getWidth() * r.getHeight() != 20) {        throw new RuntimeException();    }} //some other place Rectangle square = new Square(); g(square);

  6. Liskov Substitution Principle (LSP) • Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it • Very difficult problem!! • Further study: OOAD • Software design principals • Please google it if you are interested in it • One of the principals • Prefer Composition over inheritance • Why? Can you give an example?

  7. Delegation Aggregation Inheritance

More Related