- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
关闭。这个问题是opinion-based .它目前不接受答案。
想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它.
8年前关闭。
Improve this question
考虑 Java 中的经典示例
// Violation of Likov's Substitution Principle
class Rectangle
{
protected int m_width;
protected int m_height;
public void setWidth(int width){
m_width = width;
}
public void setHeight(int height){
m_height = height;
}
public int getWidth(){
return m_width;
}
public int getHeight(){
return m_height;
}
public int getArea(){
return m_width * m_height;
}
}
class Square extends Rectangle
{
public void setWidth(int width){
m_width = width;
m_height = width;
}
public void setHeight(int height){
m_width = height;
m_height = height;
}
}
class LspTest
{
private static Rectangle getNewRectangle()
{
// it can be an object returned by some factory ...
return new Square();
}
public static void main (String args[])
{
Rectangle r = LspTest.getNewRectangle();
r.setWidth(5);
r.setHeight(10);
// user knows that r it's a rectangle.
// It assumes that he's able to set the
// width and height as for the base class
System.out.println(r.getArea());
// now he's surprised to see that the area is 100 instead of 50.
}
}
Rectange rectange = new Square();
class Rectangle
attr_accessor :width, :height
def getArea()
@width * @height
end
end
class Square < Rectangle
def width=(number)
super(number)
@height = number
end
def height=(number)
super(number)
@width = number
end
end
s = Square.new(100)
puts s.class
s.width = 50
puts s.height
rectange.class
Square
Rectange
.
square
对象是
Square
的一个实例类(class),
rectangle
对象是
Rectangle
的一个实例类(class)
width=
是两者中的一种方法
width=
在
square
无法替换为
width=
在
rectangle
Rectangle
中使用紫罗兰色的 'width=' 方法。类来分析这个问题:
width=
在“矩形”类中
@width
和
@height
有一定的值(value)。
@width
更改为新值,
@height
保持原样。
Square
中的“宽度=”类(class)
@height
更改为新值
@height
已更改,因此未履行 promise ,因此不能继承
DBC
给我一些关于我分析这个问题的方法的建议? ?
最佳答案
LSP 仍然适用,即使在像 Ruby 这样的动态类型语言中也是如此。你的推理:
I can alway check the class of the object by using:
rectange.class
in this case, if the code in Ruby it will return
Square
so I won't treat it likeRectangle
.
Square
不是
Rectangle
从 OOP 的角度来看——一个
Square
的行为与
Rectangle
的行为不兼容.
关于java - LSP 对像 Ruby 这样的动态类型语言也有意义吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17077097/
来自 Liskov Substitution Principle - www.blackwasp.co.uk One common indication of non-compliance with
是否可以在 Windows 中使用 LSP(分层服务提供程序)更改传出数据包的目标套接字(IP 和端口)? 例如,我想将所有 HTTP 数据包重定向到代理服务器(给定 IP/端口)。这样我就可以为 W
在 objective-C 中,我可以子类化一个 View Controller ,如下所示。 class KeyboardObserverViewController: UIViewControll
提前为一个冗长的问题道歉。在这里特别感谢反馈。 . . 在我的工作中,我们做了很多关于日期范围的事情(日期周期,如果你愿意的话)。我们需要进行各种测量,比较两个日期周期之间的重叠等。我设计了一个接口(
我的处境与Steve McConnell's 非常相似在Code Complete中有提到。只有我的问题是基于 Vehicles 而 Trike 恰好是根据法律属于 Cars 类别。到目前为止,汽车只
本文实例讲述了PHP面向对象五大原则之里氏替换原则(LSP)。分享给大家供大家参考,具体如下: 替换原则由MIT计算机科学实验室的Liskov女士在1987年的OOPSLA大会上的一篇文章中提出,
当我注意到this answer时,我一直在阅读里氏替换原理。 。它有一个 Circle 和一个 ColoredCircle 类型,其中 ColoredCircle 的构造函数需要一个额外的参数; 颜
我正在寻找代码中的标志,以表明可能违反里氏替换原则。首先,我创建了一个简单的类和另一个继承它的类: public class Adder { public virtual int Add(in
我正在寻找代码中的标志,以表明可能违反里氏替换原则。首先,我创建了一个简单的类和另一个继承它的类: public class Adder { public virtual int Add(in
我有这段代码可以实现两种类型的门。一扇门有锁,另一扇没有。 Door 界面很简单: public interface Door { void open(); void close();
我有一个名为 Message 的基类,如下所示: public abstract class Message { protected int m_id; protected bool
为了使我的代码可测试,我正在尝试遵守 Liskov's substitution principle让我的 SwiftUI View 依赖于协议(protocol)而不是具体类型。这使我可以轻松地交换
我更多是出于好奇而不是真正关心它 ,但我一直想知道 JavaScript 事件系统是否违反了 Liskov substitution principle (LSP)或不。 调用 EventTarget
序言 是我太久没发了吗?昨天没人看,那么今天来点特别的~ 不仅把好看的视频全部pa下来,咱们还要实现自动评论、点赞、关注三连~ 宝,你也可以顺手给我个三连吗?给你个摸摸大~ 抓包分析流程 我写成了文档
我的 Neovim LSP 错误消息没有红色: 这是我的 vimrc 文件。我真的不知道是什么问题。 syntax on set guicursor= set relativenumber set n
LSP与协方差和逆变之间有什么关系?有什么关系吗? LSP 是协方差的一种形式吗? 最佳答案 我不会这么说Liskov's Substitution Principle是协方差的一种“形式”,就像泛型
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 8年前关闭。 Improve this
在emacs中,使用gopls但在我的路径上找不到go。但它在那里,我可以运行它。向社区伸出援手,看看我是否缺少任何东西。我可以毫无问题地运行基本的gin服务器。 使用文档中的config。 输出:
我正在开发 LSP,我需要知道确切的 URL 是什么。 例如,假设用户正在使用 Firefox 浏览页面。我想获取页面的完整 URL,无论是 HTTP 还是 HTTPS。我该怎么做? 我可以通过获取
有谁知道如何调试微软平台SDK提供的LSP示例?我阅读了有关此主题的内容,发现示例 dt_dll 生成了一个 DLL 文件,该文件可与已检查版本的 ws2_32.dll 一起使用。 问题是他们没有说明
我是一名优秀的程序员,十分优秀!