设计模式(六)之里氏替换原则

里氏替换原则,为继承定义规范。

里氏替换原则有如下特点:

代码共享,减少创建类的工作量

提高代码的重用性

提高代码的可扩展性

提高产品代码的开放性

继承侵入性 只要继承,必须拥有父类的内容

降低代码的灵活性,子类必须拥有父类的属性和方法

增强耦合性。

里氏替换原则:

有一功能P1,由类A完成。现需要将功能P1进行扩展,扩展后的功能为P,其中P由原功能P1与新功能P2组成。新功能P由类A的子类B来完成,则子类B在完成新功能P2的同时,有可能会导致原有功能P1发生故障。当使用继承时,遵循里氏替换原则。类B继承类A时,除添加新的方法完成新增功能P2外,尽量不要重写父类A的方法,也尽量不要重载父类A的方法。

举个例子:

我这里有一个长方形类,一个正方形类,正方形是一个特殊的长方形。那么正方形类继承自长方形类:

代码如下所示:

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Displace
{
    /// <summary>
    /// 高层模块:调用长方形及正方形类
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle rectangle = new Rectangle();
            rectangle.SetHeight(10);
            rectangle.SetWidth(20);
            Console.WriteLine(rectangle.GetHeight());
            Console.WriteLine(rectangle.GetWidth());
  
            Console.WriteLine("------------  我是分割线  -------------");
  
            Square square = new Square();
            square.SetHeight(10);
            square.SetWidth(20);
            Console.WriteLine(square.GetHeight());
            Console.WriteLine(square.GetWidth());
  
            Console.ReadKey();
        }
    }
}

长方形类:Rectangle.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Displace
{
    /// <summary>
    /// 长方形类
    /// </summary>
    public class Rectangle
    {
        public double height;
        public double width;
  
        public void SetHeight(double height)
        {
            this.height = height;
        }
  
        public double GetHeight()
        {
            return height;
        }
  
        public void SetWidth(double width)
        {
            this.width = width;
        }
  
        public double GetWidth()
        {
            return width;
        }
    }
}
 

正方形类:Square.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Displace
{
    /// <summary>
    /// 正方形类
    /// </summary>
    public class Square: Rectangle
    {
        public void SetHeight(double height)
        {
            this.height = height;
            this.width = height;
        }
  
        public double GetHeight()
        {
            return height;
        }
  
        public void SetWidth(double width)
        {
            this.height = width;
            this.width = width;
        }
  
        public double GetWidth()
        {
            return width;
        }
  
        /// <summary>
        /// 求面积
        /// </summary>
        public double GetArea()
        {
            return width * height;
        }
    }
}

输出结果如下图所示:


1782fd89779042708c6d57e5176ade31_tplv-k3u1fbpfcp-zoom-1.png

输出的结果明显是不对的,我调用父类与子类的统一方法输出的结果应该是一致的。所以上面的栗子不符合里氏替换原则。

里氏替换原则:
只要有父类出现的地方,都可以用子类来替代,而且不会出现任何错误和异常。但是反过来则不行,有子类出现的地方,不能用其父类替代。
包含以下四种约束:
1:子类必须实现父类的抽象方法,但不得重写(覆盖)父类的非抽象(已实现)方法。
有时候父类有多个子类,但在这些子类中有一个特例。要想满足里氏替换原则,又想满足这个子类的功能时,有的伙伴可能会修改父类的方法。但是,修改了父类的方法又会对其他的子类造成影响,产生更多的错误。这是怎么办呢?我们可以为这个特例创建一个新的父类,这个新的父类拥有原父类的部分功能,又有不同的功能。这样既满足了里氏替换原则,又满足了这个特例的需求。
2:子类中可以增加自己特有的方法。
3:当子类覆盖或实现父类的方法时,方法的前置条件(即方法的形参)要比父类方法的输入参数更宽松。(子类的参数范围要比父类的参数范围大)
4:当子类的方法实现父类的抽象方法时,方法的后置条件(即方法的返回值)要比父类更严格。

我上面的那段程序很显然是不符合里氏替换原则的,那么上边我们那段程序该怎么修改呢,我们创建一个接口,接口中包含获取宽度及高度的两个方法,长方形类和正方形类分别实现这个接口,那么其二者现在就为同级,父类可以的地方,他们二者都可以。下边是我修改之后的代码:

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Displace
{
    /// <summary>
    /// 高层模块:调用长方形及正方形类
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle rectangle = new Rectangle();
            rectangle.SetHeight(10);
            rectangle.SetWidth(20);
            Console.WriteLine(rectangle.GetHeight());
            Console.WriteLine(rectangle.GetWidth());
  
            Console.WriteLine("------------  我是分割线  -------------");
  
            Square square = new Square();
            square.SetHeight(10);
            square.SetWidth(20);
            Console.WriteLine(square.GetHeight());
            Console.WriteLine(square.GetWidth());
  
            Console.ReadKey();
        }
    }
}

Rectangle.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Displace
{
    /// <summary>
    /// 长方形类
    /// </summary>
    public class Rectangle: Interface1
    {
        public double height;
        public double width;
  
        public void SetHeight(double height)
        {
            this.height = height;
        }
  
        public double GetHeight()
        {
            return height;
        }
  
        public void SetWidth(double width)
        {
            this.width = width;
        }
  
        public double GetWidth()
        {
            return width;
        }
    }
}

Square.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Displace
{
    /// <summary>
    /// 正方形类
    /// </summary>
    public class Square: Interface1
    {
        public double height;
        public double width;
        public void SetHeight(double height)
        {
            this.height = height;
            this.width = height;
        }
  
        public double GetHeight()
        {
            return height;
        }
  
        public void SetWidth(double width)
        {
            this.height = width;
            this.width = width;
        }
  
        public double GetWidth()
        {
            return width;
        }
  
        /// <summary>
        /// 求面积
        /// </summary>
        public double GetArea()
        {
            return width * height;
        }
    }
}

父类接口:Interface1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Displace
{
    interface Interface1
    {
        double GetHeight();
        double GetWidth();
    }
}
 

这个程序基本上就实现了上边所说的四个约束,例子有点烂,但是基本上融汇了里氏替换原则的思想。

说到底,里氏替换原则就是对继承的约束~

欢迎访问个人博客
https://guanchao.site

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容