- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
你可以有一个父类(super class) Shape,Square 和 Rectangle 是两个子类,但是你可以有 Square 子类 Rectangle,因为 Square 是一个四边相等的特殊 Rectangle?
我试图用一个原则来检验,只要使用父类(super class),它应该可以被它的子类替换,但如果使用子类,它可能无法被它的父类(super class)替换。看起来很好,但我总觉得 Square 不能是 Rectangle 的 child ,因为某种原因?谁能给我一些启示?
最佳答案
您指的是Liskov Substition Principle
Substitutability is a principle in object-oriented programming. It states that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may be substituted for objects of type T) without altering any of the desirable properties of that program (correctness, task performed, etc.).
A typical example that violates LSP is a Square class that derives from a Rectangle class, assuming getter and setter methods exist for both width and height. The Square class always assumes that the width is equal with the height. If a Square object is used in a context where a Rectangle is expected, unexpected behavior may occur because the dimensions of a Square cannot (or rather should not) be modified independently. This problem cannot be easily fixed: if we can modify the setter methods in the Square class so that they preserve the Square invariant (i.e., keep the dimensions equal), then these methods will weaken (violate) the postconditions for the Rectangle setters, which state that dimensions can be modified independently. Violations of LSP, like this one, may or may not be a problem in practice, depending on the postconditions or invariants that are actually expected by the code that uses classes violating LSP. Mutability is a key issue here. If Square and Rectangle had only getter methods (i.e., they were immutable objects), then no violation of LSP could occur.
关于design-patterns - Square 和 Rectangle 可以继承 Shape,但是 Square 可以继承 Rectangle 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11444984/
我一直在努力学习 TensorFlow,我注意到不同的函数用于相同的目标。例如,为了平方变量,我看到了 tf.square()、tf.math.square() 和 tf.keras.backend.
我正在创建一个应用程序,我需要在其中显示平方根符号和数字。例如 /¯¯¯¯¯¯¯¯¯¯¯ \/ (v^2) / T 我也希望显示数字的平方。任何人都可以帮助显示此类文本。此文本来自数据库并且可以
我自己无法解决以下问题,所以我搜索并找到了一个可行的算法。有人可以向我解释该算法背后的思维过程、数学或其他内容吗? 类型:https://www.codewars.com/kata/square-in
是否可以使用 Square Connect API 访问有关商家客户的任何信息?最理想的信息是客户为收据输入的电子邮件地址,但某些类型的唯一客户 ID 可以很好地确定回头客。 查看 Square Co
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我正在尝试使用 Font Awesome 堆叠来实现这一点 我试过两种方法,但都很丑。问题是 fa-square 和 fa-square-o 大小不同,所以当你堆叠它们时,它们不会对齐! my fid
这个惊人的code golf answer to 这个数字是 Loeschian 吗?整体而言: Python, 49 bytes lambda n:0in[(n-3*i*i+0j)**.5%1for
这个问题在这里已经有了答案: Is floating point math broken? (31 个回答) 去年关闭。 #include #include using namespace std
我有一个名为Complex的类,它具有实数和虚数的访问器方法,以及将复数表示为字符串的方法和获取复数大小的方法。我在实现最后三个方法 square()(对复数求平方)、modulusSquared()
有没有办法使用 Square Connect API 获取通过网络钩子(Hook)发送的付款 ID 并找到关联的订单 ID? 我发现文档中列出的付款对象 (https://docs.connect.s
你可以有一个父类(super class) Shape,Square 和 Rectangle 是两个子类,但是你可以有 Square 子类 Rectangle,因为 Square 是一个四边相等的特殊
我正在尝试在 Google Search Console 中为 Weebly/Square 网站使用 PageSpeed Insights 并收到错误消息: 灯塔返回错误:NOT_HTML。提供的页面
我有一个包含 A 列和 B 列的数据框。我想创建第三列,它是两列平方和的平方根。 以下是我的示例数据: A B 10 7 10 8 9 8 10 11 13 5 3 0 12 8
给定以下二维点: 213 106.8 214 189 214 293.4 213 324 223 414 我想找到穿过它们的最小二乘垂直轴线的方程。我的计划是得到一个线方程,这样我就可以测试后续点到最
我的 WPF 项目中有一个 UniformGrid 对象,它有 2 行和 3 列,其宽度和高度设置为自动(两个对齐方式都设置为拉伸(stretch))。 这个网格将容纳 6 个正方形,我想尽可能多地填
我发布此问题是因为,我的代码没有在正确的位置停止迭代。谁能让我确定哪里出了问题? 一切工作正常(我一直认为这是现实中的错误。) 问题: 1)在每次迭代中,我都在最小化不幸的是,它没有正确地最小化(非常
“方 block ” 输入 : 棋盘大小 m × n (m, n ∈ {1,...,32}),三元组列表 (i, j, k) , 其中 i ∈ {1,...,m}, j ∈ {1,...,n}, k
我正在尝试为 Square Connect API 开发一个包装器。我正在寻找沙盒帐户或将测试数据导入新帐户的方法,以便我可以快速开始开发方面的工作。 谢谢! 最佳答案 为了供从 Google 登陆这
我有一个任务,我发现它非常困难。任何帮助将不胜感激。 通过创建 Shape 类 Circle、Square 和 Triangle 来构建层次结构。对于这些派生类,创建默认构造函数和构造函数,其参数可以
题目地址: https://leetcode.com/problems/maximal-square/description/ 题目描述 Given a 2D binary matrix fill
我是一名优秀的程序员,十分优秀!