设计原则 - 里氏替换原则

六大设计原则之 里氏替换原则 (Liskov Substitution Principle | LSP) 目录 前言 里氏替换原则 定义 场景 特点 优点 栗子 正例 特例1 特例2 前言 OO(Object Oriented 面向对象) 设计的六大原则:OCP(开闭原则),DIP(依赖倒置原则),ISP(接口隔离原则),LSP(里氏替换原则),SRP(单一职责原则),LKP(迪米特法则) 里氏替换原则 定义 面向对象的基本设计原则之一,里氏替换原则中说,任何基类可以出现... 阅读详情
里氏替换原则(Liskov Substitution Principle,LSP)

1.定义一(最正宗的定义):

  • 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 then S is subtype of T.
  • 如果对每一个类型为S的对象o1,都有类型为T的对象o2,使得以T定义的所有程序P在所有的对象o1都替换为o2时,程序的行为没有发生变化,那么类型S是类型T的子类型。

2.定义二:

  • Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.
  • 所有引用基类的地方必须能透明的使用其子类的对象。(通俗点:只要父类出现的地方子类就可以出现,而且替换不会产生任何错误或异常,使用者根本就不需要知道是父类还是子类。反过来就不行了,有子类的地方,父类未必能适应)

里氏替换原则为良好的继承定义了规范:

  • 子类必须完全实现父类的方法
  • 子类可以有自己的个性
  • 覆盖或实现父类的方法时输入参数可以被放大
  • 覆写或实现父类的方法时输出结果可以被缩小
【愚公系列】面向对象设计原则(三)-里氏替换原则(Liskov Substitution Principle or LSP) 面向对象设计原则是一些通用的软件设计原则,用于指导软件设计人员开发高质量、可扩展、可维护的软件系统。这些原则的作用如下:提高软件系统的可维护性:遵循面向对象设计原则可以将代码分解为更小、更专注的模块,从而降低代码的复杂性和耦合度,提高系统的可读性和可维护性。提高软件系统的可扩展性:开放封闭原则可以帮助软件系统更容易地适应变化,降低对现有代码的影响,从而提高软件系统的可扩展性和灵活性。 阅读详情

相关推荐

视觉SLAM十四讲--第13讲 实践:设计SLAM系统(最详细的代码调试运行步骤)

最详细的代码调试运行避坑说明,也包含了我自己对代码的理解,如有理解错误的地方,欢迎指正交流。

记录和分享 1万+

六大设计原则 (1)

1、单一职责原则 There should never be more than one reason for a class change. 2、里氏替换原则 第一种定义 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

永不止步!!!! 489

安卓手机必备:Auto.js快手极速版自动刷视频避坑指南(含最新3.0脚本)

本文是一份安卓自动化工具Auto.js的全面实践指南,重点解析了如何搭建稳定运行环境、配置关键权限以及管理脚本。针对快手极速版等应用的自动化需求,文章详细介绍了设备选择、Auto.js版本对比、各品牌手机后台保活设置,并强调了脚本安全与调试技巧,帮助用户有效避坑,实现自动化任务的稳定执行。

i7j8k9l的博客 782

设计模式---->里氏替换原则

里氏替换原则【Liskov Substitution Principle】 1、定义 第一个定义,最正宗的定义:If for each object o1 of type S there is an object o2 of type T suchthat for all programs P defined in terms of T, the behavior of P is unchan...

weixin_34019929的博客 140

数字IC后端新手必看:Innovus从安装到跑通第一个设计的完整流程(附常用命令清单)

本文为数字IC后端设计新手提供了Cadence Innovus工具的完整实战指南。详细介绍了从环境搭建、软件安装与许可证配置,到加载网表、库文件和约束,并逐步完成布局规划、单元布局、时钟树综合、布线及物理验证的核心使用流程。文章最后附有常用Tcl命令清单,帮助初学者快速上手并跑通第一个设计。

vscode5coder的博客 1239

张正友标定法-MATLAB实验

该方法最早由微软研究院的Zhengyou Zhang教授提出,1998年发表在IEEE TRANSACTIONS ON PATTERNANALYSIS AND MACHINE INTELLIGENCE,论文题目为:AFlexible New Technique for Camera Calibration。 (一)标定平面到图像平面的单应性 单应性(homography):在计算机视觉中被定义

qq_37845624的博客 8290

里氏替换原则

一.什么是里氏替换原则 里氏替换原则最早是由Barbara Liskov在1987年OOPSLA上提出的。她指出:“若对每个类型S的对象o1,都存在一个类型T的对象o2,使得在所有针对T编写的程序P中,用o1替换o2后,程序P的行为不变,则S是T的子类型。” 一个软件实体如果适用一个父类的话,那一定适用于其子类,所有引用父类的方法必须能透明地使用其子类的对象,子类对象能够替换父类对象,而程序逻辑...

qq_38844728的博客 6188

GEE入门之路(从【初学者】角度,大白话)——附海面风速插值代码

非常初级的GEE入门教程!大白话!有手就行!附海面风速插值代码,克里金插值,时间插值。

weixin_46812066的博客 4086

里氏替换原则(六大设计原则)

里氏替换原则里氏替换原则介绍 里氏替换原则 介绍 依赖于继承,多态两大特性 所有引用基类的地方必须能透明的使用其子类的对象,即只要父类出现的地方子类就可以出现,并且替换为子类也不会产生任何错误或异常,使用者根本不知道是父类还是子类 子类必须完全的实现父类的方法 ...

熬夜看书的博客 257

Windows10环境下激活虚拟环境报错【source : 无法将“source”项识别为 cmdlet、函数、脚本文件或可运行程序的名称】

在使用Django创建Web程序前,需要创建一个虚拟的工作环境,在使用【source ll_env/bin/activate】命令激活虚拟环境时报错:source : 无法将“source”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。所在位置 行:1 字符: 2。

提供C#相关开发 、云计算、运维测试、网络安全相关技术分享与服务,有意向可联系。 1万+

Spring AI Alibaba实战训练营-28 手把手教你用SAA+ Nacos 构建分布式 MCP 应用

本文介绍如何使用Spring AI Alibaba和Nacos构建分布式MCP应用,实现AI模型与工具服务的动态发现和调用。主要内容包括:1) 项目架构设计,包含MCP Server、Nacos注册中心和MCP Client三个核心组件;2) 环境准备要求,如Java 17+、Maven 3.6+、Nacos 3.1.0+等;3) 详细实施步骤:启动Nacos服务、注册MCP服务端、配置MCP网关聚合服务、设置MCP客户端发现机制。通过这种架构,开发者可以轻松扩展新工具服务,无需修改客户端代码,实现服务动态

program哲学 311

七大原则——里氏替换原则

里氏替换原则的定义 里氏替换原则(Liskov Substitution Principle,LSP)由麻省理工学院计算机科学实验室的里斯科夫(Liskov)女士在 1987 年的“面向对象技术的高峰会议”(OOPSLA)上发表的一篇文章《数据抽象和层次》(Data Abstraction and Hierarchy)里提出来的,她提出:继承必须确保超类所拥有的性质在子类中仍然成立(Inherit...

m0_37654408的博客 1万+

SAP 行业扫盲系列之一:SAP 从业者的分类

本文简单介绍了 SAP 从业者的分类,限于笔者狭隘的视角,可能存在遗漏或者不恰当的地方,希望大家在评论区指出。 那些在知乎咨询上向我提问关于进入 SAP 行业建议的朋友们,也请大家提问时注明自己想具体从事 SAP 行业哪一类的工作,这样才能得到更让你们满意的回复。感谢阅读。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1589

OO设计原则 -- Liskov Substitution Principle: OO设计的LSP里氏替换原则

概要  Functions that use pointers or references to base classesmust be able to use objects of derived classes without knowing it. 所有引用基类的地方必

亿言 7108

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

设计原则之-里氏替换原则(Liskov‘s Substitution Principle,LSP)

定义 里氏替换原则是Barbara Liskov1与1988年提出来的。原文是: What is wanted here is something like the following substitution property: If for each object of type S there is an object of type T such that for all program...

jaune162的专栏,最新动态请关注:https://jaune162.blog 725

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

[设计原则3] LSP 里氏替换原则

LSP原则定义: 定义1:如果对每一个类型为 T1的对象 o1,都有类型为 T2 的对象o2,使得以 T1定义的所有程序 P 在所有的对象 o1 都代换成 o2 时,程序 P 的行为没有发生变化,那么类型 T2 是类型 T1 的子类型。 定义2:所有引用基类的地方必须能透明地使用其子类的对象。

u013150151的专栏 1182

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

设计原则-里氏替换原则(Liskov Substitution Principle LSP)

意图 规定继承 定义1:如果对每一个类型为 T1的对象 o1,都有类型为 T2 的对象o2,使得以 T1定义的所有程序 P 在所有的对象 o1 都代换成 o2 时,程序 P 的行为没有发生变化,那么类型 T2 是类型 T1 的子类型。 定义2:所有引用基类的地方必须能透明地使用其子...

230

【设计原则】里氏替换原则(LSP):构建稳健继承体系的黄金法则

里氏替换原则(Liskov Substitution Principle, LSP)是面向对象设计SOLID原则中的"L",由Barbara Liskov在1987年提出。所有引用基类(父类)的地方必须能透明地使用其子类的对象子类必须完全实现父类的抽象方法子类可以扩展父类功能但不能改变原有行为子类方法的前置条件不应强于父类子类方法的后置条件不应弱于父类优先使用组合而非继承保持继承层次扁平化使用设计模式:策略模式模板方法模式装饰器模式定期进行代码审查编写契约测试。

OpenSeek的博客 1990

OO设计原则 -- Liskov Substitution Principle: 里氏替换原则(LSP)

概要 &amp;nbsp;Functions that use pointers or references to base classesmust be able to use objects of derived classes without knowing it. 所有引用基类的地方必须能透明地使用其子类的对象。 &amp;nbsp;即: ◇...

行云间 820

OOD设计原则之里氏替换原则(LSP)

里氏替换原则(Liskov Substitutiion Principle,LSP)被称作继承复用的基石,它的提出甚至要早于OCP。不过遗憾的是,由于对这一原则的理解各不相同,经过多次的翻译、转述,LSP成了OOD设计原则中争议最多的话题之一。其实早在1987年的OOPSLA大会上,麻省理工学院(MIT)计算机科学实验室的Liskov女士就发表了经典文章Data Abstraction and

Brookes的专栏 8736

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

里氏替换原则简称LSP。定义:所有引用基类的地方必须能够透明地使用其子类的对象。所谓对象是一组状态和一系列行为的组合。状态是对象的内在特性,行为是对象的外在特性。LSP所表述的就是在同一个继承体系中的对象应该有共同的行为特征。我们在设计对象时是按照行为进行分类的,只有行为一致的对象才能抽象出一个类来。因此,如果说鸵鸟和企鹅属于鸟类的话,就违背了里氏替换原则。如何规范地遵从里氏替换原则:1 子类必须...

ruguowoshiyu的博客 1520

设计原则之里氏替换原则(LSP)

简介 里氏替换原则是在做继承设计时需要遵循的原则,不遵循了 LSP 的继承类会带来意想不到的问题。 定义 里氏替换原则(Liskov Substitution Principle) 是由 Barbara Liskov 在 1987 年提出来的,Liskov 是她的姓,国内翻译成 里氏。 原则...

chj287522860的博客 303

IOS设计模式的六大设计原则之里氏替换原则(LSP,Liskov Substitution Principle)

定义 里氏替换原则的定义有两种,据说是由麻省理工的一位姓里的女士所提出,因此以其名进行命名。 定义1:如果对一个类型为T1的对象o1,都有类型为T2的对象o2,使得以T1所定义的程序P中在o1全都替换成o2时,程序的行为不发生任何变化,那么T2为T1的子类。 定义2:所有引用父类的地方都必须能够透明地使用其子类对象。 定义解读 其实两个定义所表达的意思都相...

weixin_33851604的博客 357
上一篇: 设计原则 - 单一原则
下一篇: 设计原则 - 依赖倒置原则
香草粥
博客等级 码龄10年 0粉丝 16原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值