using System;
using System.Collections.Generic;
using System.Text; namespace LiskovSubstitutionPrinciple
{
//里氏替换原则(Liskov Substitution Principle) LSP
//If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T,
//the behavior of P is unchanged when o1 is substituted for o2 than S is a subtype of T.
//(如果对每一个类型为S的对象o1,都有类型为T的对象o2,使得以T定义的所有程序P在所有对象o1都替换成o2时,程序P的行为没有发生变化,那么类型S是类型T的子类型。)
//通俗点讲,只要父类能出现的地方子类就可以出现,而且替换为子类也不会产生任何错误或异常,使用者根本不需要知道是父类还是子类。
//但是反过来就不行了,有子类出现的地方,父类未必能适应(因为子类可能存在自己的方法属性,父类并不知道呀)。
class Program
{
static void Main(string[] args)
{
Square square = new Square();
//隐式转换,只要父类Shape能出现的地方子类就可以出现
Shape shape = new Rectangle(, );//替换为子类也不会产生任何错误或异常,这是因为子类肯定继承了父类的方法属性。
//显示转换
Shape shape2 = (Shape)square; //只要父类Shape能出现的地方子类square、rectangle就可以出现
GetShapeArea(shape);
GetShapeArea(shape2);
GetShapeArea(square); //只要父类IDisposable能出现的地方子类square、rectangle就可以出现
DisposeShape(shape);
DisposeShape(shape2);
DisposeShape(square);
} static public void GetShapeArea(Shape shape)
{
//得益于LSP,我们不用知道实际的子类是什么,提高了扩展性
Console.WriteLine(shape.Name + "的面积为" + shape.GetArea());
} //同理,继承接口也是一种继承,暂且认为接口IDisposable也是父类吧~
static public void DisposeShape(IDisposable obj)
{
obj.Dispose();
}
} /// <summary>
/// 有抽象方法的类一定是抽象类,抽象类自身不能被实例化。
/// 继承增强了耦合,父类在进行改动时,要考虑子类的修改,不然全部子类都要重构!
/// </summary>
public abstract class Shape : IDisposable
{
private string name; public string Name
{
get { return name; }
set { name = value; }
}
private int width; public int Width
{
get { return width; }
set { width = value; }
}
private int height; public int Height
{
get { return height; }
set { height = value; }
} //子类一定要实现抽象方法
public abstract int GetArea(); public void Dispose()
{
Console.WriteLine(Name + "释放掉了");
}
} /// <summary>
/// 正方形,继承Shape使得我们代码重用,减少工作量。
/// </summary>
public class Square : Shape
{
public Square(int width)
{
Name = "Square";
Width = width;
//继承虽好,但是只要继承,就必须拥有父类的所有属性和方法,这里的Height对于正方形来说其实没有存在的必要,因为四边都相等呐。
Height = width;
} //我们只需实现抽象方法,其他东西一律继承!~
public override int GetArea()
{
return Width * Height;
}
} /// <summary>
/// 长方形
/// </summary>
public class Rectangle : Shape
{
public Rectangle(int width, int height)
{
Name = "Rectangle";
Width = width;
Height = height;
} public override int GetArea()
{
return Width * Height;
}
}
}

里氏替换原则(Liskov Substitution Principle) LSP的更多相关文章

  1. 里氏代换原则(Liskov Substitution Principle,LSP)

    第一种定义: 如果对每一个类型为S的对象o1,都有类型为T的对象o2,使得以T定义的所有程序P在所有的对象o1都代换为o2,程序P的行为没有发生变化,那么类型S是类型T的子类型. 第二种定义: 所有引 ...

  2. 里氏替换原则(Liskov Substitution Principle,LSP)

    肯定有不少人跟我刚看到这项原则的时候一样,对这个原则的名字充满疑惑.其实原因就是这项原则最早是在1988年,由麻省理工学院的一位姓里的女士(Barbara Liskov)提出来的. 定义1:如果对每一 ...

  3. 设计模式六大原则(二):里氏替换原则(Liskov Substitution Principle)

    里氏替换原则(LSP)由来: 最早是在 妖久八八 年, 由麻神理工学院得一个女士所提出来的. 定义: 1:如果对每一个类型为 T1的对象 o1,都有类型为 T2 的对象o2,使得以 T1定义的所有程序 ...

  4. 面象对象设计原则之三:里氏替换原则(The Liskov Substitution Principle,LSP)

    里氏代换原则由2008年图灵奖得主.美国第一位计算机科学女博士Barbara Liskov教授和卡内基·梅隆大学Jeannette Wing教授于1994年提出.其严格表述如下:如果对每一个类型为S的 ...

  5. 设计模式原则(2)--Liskov Substitution Principle(LSP)--里氏替换原则

    1.定义: 所有引用基类(父类)的地方必须能透明地使用其子类的对象.这一原则与继承紧密相关.如果对每一个类型为 T1的对象 o1,都有类型为 T2 的对象o2,使得以 T1定义的所有程序 P 在所有的 ...

  6. 面向对象设计原则 里氏替换原则(Liskov Substitution Principle)

    里氏替换原则(Liskov Substitution Principle LSP)面向对象设计的基本原则之一. 里氏替换原则中说,任何基类可以出现的地方,子类一定可以出现. LSP是继承复用的基石,只 ...

  7. 【面向对象设计原则】之里氏替换原则(LSP)

    里氏代换原则由2008年图灵奖得主.美国第一位计算机科学女博士Barbara Liskov教授和卡内基·梅隆大学Jeannette Wing 教授于1994年提出,所以使用的是这位女博士的性命名的一个 ...

  8. 面向对象五大原则_1.单一职责原则&amp;2.里氏替换原则

    单一职责原则:Single Responsibility Principle (SRP) 一个类.仅仅有一个引起它变化的原因.应该仅仅有一个职责.每个职责都是变化的一个轴线.假设一个类有一个以上的职责 ...

  9. 里氏替换原则(Liskov Substitution Principle)

    开放封闭原则(Open Closed Principle)是构建可维护性和可重用性代码的基础.它强调设计良好的代码可以不通过修改而扩展,新的功能通过添加新的代码来实现,而不需要更改已有的可工作的代码. ...

  10. Java设计原则之里氏替换原则

    里氏代换原则由2008年图灵奖得主.美国第一位计算机科学女博士Barbara Liskov教授和卡内基·梅隆大学Jeannette Wing教授于1994年提出.其严格表述如下:如果对每一个类型为S的 ...

随机推荐

  1. (转)XSS危害——session劫持

    原文地址:http://www.cnblogs.com/dolphinX/p/3403027.html 在跨站脚本攻击XSS中简单介绍了XSS的原理及一个利用XSS盗取存在cookie中用户名和密码的 ...

  2. Schwartz kernel theorem施瓦兹核定理

    In mathematics, the Schwartz kernel theorem is a foundational result in the theory of generalized fu ...

  3. .net在线HTML编辑器

    //在线网页编辑器, <script> var editor2 = new baidu.editor.ui.Editor({//实例化编辑器 initialContent: '', min ...

  4. RabbitMQ单机集群搭建出现Error: unable to perform an operation on node 'rabbit1@ClusterNode1'

    参考链接:https://www.cnblogs.com/daryl/archive/2017/10/13/7645749.html 全部步骤和参考链接相同. 前八部都正常,在第九步会报错Error: ...

  5. javascript framework js常用框架

    js常用框架 一.node.js   二.angularjs.js   三.react.js   四.webpack.js   五.flux.js   六.vue.js   七.bootstrap   ...

  6. CI框架集成Smarty

    1.下载smarty源码包,解压放置于项目目录 libriaries中 2.在libraries中建立Cismarty.php ,填写如下代码 <?php if(!defined('BASEPA ...

  7. 获取oracle 库所有表名

    (mybatis多参)

  8. 871. Minimum Number of Refueling Stops

    A car travels from a starting position to a destination which is target miles east of the starting p ...

  9. F题(水题)

    给出一个有N个数的序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有数中,最大的数是多少.   例如: 1 7 6 3 1.i = 1, j = 3,对应的数为7 6 3,最大的数为7. ...

  10. 3月份GitHub上最热门的Java开源项目

    今天,我们来盘点3月份GitHub上最热门的Java项目的时候了,如果你每月都有关注猿妹发布的排行榜,那么本月的Java项目对你来说一定不陌生,这些都是曾经多次出现在榜单中的项目: 1 advance ...