Richter Replacement principle (Liskov Substitution Principle) LSP

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceliskovsubstitutionprinciple{//Richter Replacement principle (Liskov Substitution Principle) LSP//the If for each object O1 of type S There are an object O2 of type T such so for all programs P defined in terms of T, //The behavior of P is unchanged when O1 are substituted for O2 than S is a subtype of T. //(if for each object of type S O1, there is an object of type T O2, so that all program P defined in T is replaced with O2 for all objects O1, the behavior of the program P does not change, then the type S is a subtype of type T. )//Popular point, as long as the parent class can appear where the subclass can appear, and the substitution of subclasses will not produce any errors or exceptions, the consumer does not need to know whether it is a parent class or a subclass. //but in the opposite, there are subclasses where the parent class may not be able to adapt (because the subclass might have its own method attribute, the parent does not know it).     classProgram {Static voidMain (string[] args) {Square Square=NewSquare ( -); //implicit conversions, as long as the parent class shape can appear where subclasses can appearShape shape =NewRectangle ( -, the);//substituting a subclass does not produce any errors or exceptions because the subclass must inherit the method property of the parent class. //Show TransformationsShape Shape2 =(Shape) square; //as long as the parent shape can appear where the subclass square, rectangle can appearGetshapearea (Shape);            Getshapearea (SHAPE2);            Getshapearea (square); //as long as the parent class IDisposable can appear, the sub-class square, rectangle can appearDisposeshape (Shape);            Disposeshape (SHAPE2);        Disposeshape (square); }        Static  Public voidGetshapearea (Shape shape) {//thanks to the LSP, we don't need to know what the actual subclass is and improve the extensibilityConsole.WriteLine (Shape. Name +"the area is"+shape.        Getarea ()); }        //Similarly, the inheritance interface is also a kind of inheritance, think of interface IDisposable is also the parent class bar ~        Static  Public voiddisposeshape (IDisposable obj) {obj.        Dispose (); }    }    /// <summary>    ///classes with abstract methods must be abstract classes, and abstract classes cannot be instantiated by themselves. ///The inheritance enhances the coupling, when the parent class changes, must consider the subclass the modification, otherwise all subclasses must reconstruct! /// </summary>     Public Abstract classshape:idisposable {Private stringname;  Public stringName {Get{returnname;} Set{name =value;} }        Private intwidth;  Public intWidth {Get{returnwidth;} Set{width =value;} }        Private intheight;  Public intHeight {Get{returnheight;} Set{height =value;} }        //subclasses must implement abstract methods         Public Abstract intGetarea ();  Public voidDispose () {Console.WriteLine (Name+"it's released."); }    }    /// <summary>    ///squares, inheriting shape allows us to reuse code and reduce workload. /// </summary>     Public classSquare:shape { PublicSquare (intwidth) {Name="Square"; Width=width; //inheritance is good, but as long as inheritance, you must have all the properties and methods of the parent class, where the height of the square is not actually necessary, because the four sides are equal. Height =width; }        //we only need to implement abstract methods, all other things inherit! ~         Public Override intGetarea () {returnWidth *Height; }    }    /// <summary>    ///Rectangular/// </summary>     Public classRectangle:shape { PublicRectangle (intWidthintheight) {Name="Rectangle"; Width=width; Height=height; }         Public Override intGetarea () {returnWidth *Height; }    }}

Richter Replacement principle (Liskov Substitution Principle) LSP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.