design patterns - Square and Rectangle can subclass Shape, but can Square subclass Rectangle? -


You have a super class size, square and two rectangular kids, but can you square subclass be rectangle because Square is a special Rectangle is equal to four sides?

I have tried to use the theory to check that whenever a superclass is used, it should be able to replace it by its child's class, but if the use of a child class It can not be able to replace it by its super class, it looks like it's ok, but I always feel that squire is not a child of rectangle for any reason Awareness is? Can someone give me some light?

You

The colonial capacity is a principle in object-oriented programming. . It states that, in a computer program, if TT is a subtype, then T-type objects can be replaced with type S objects (i.e., the type type of the type type T Can be substituted for the object) that desirable property of that program (purity, execution, etc.)

Specific to your question:

A common example of a violation of LSP is the square class that comes from a rectangle, it is considered That the missing and setter method exists for both the post and height. Class class always believes that the width is equal to height if the context of a class object is used in any context, where a rectangle is expected, then unexpected behavior can be because the dimensions of a square are not independently modified. Can not (or should not be) this problem can be easily judged: if we can modify setter methods in square square so that they If the square is immutable (i.e., keep the amplitude equal), then this method will weaken the panoramic conditions for rectangular satellites, tell that the dimension can be modified independently, violation of LSP, in this way, There can be no problem in dealing with compliance or nonconformities, which is actually the expected code that uses classes that violate LSP. Yes. Variability is an important issue. If there were only recipients methods in the square and rectangle (i.e., they were irreversible items), then there could be no violation of the LSP.

In plain English , you should not use a square where a rectangle is expected because the behavior of a square is not rectangular. If someone is trying to use an example of a rectangle, but it is actually an example of a square, then they can set the width and be surprised that the height automatically changes (unexpected side effects) .

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -