当前位置:网站首页>方法重写(Override)
方法重写(Override)
2022-06-28 10:51:00 【Hello_xzy_Word】
方法重写(Override)
“两同两小一大”规则
方法的重写需要遵循“两同两小一大”规则:
- “两同”:方法名相同,参数列表相同
- “两小”:子类方法返回值类型应该比父类方法返回值类型更小或相等;子类方法声明抛出的异常应该比父类方法声明抛出的异常更小或相等。
- “一大”:子类方法的访问权限应该比父类方法更大或相等。
里氏替换原则
里氏替换原则(Liskov Substitution Principle,LSP)由麻省理工学院计算机科学实验室的里斯科夫(Liskov)女士在 1987 年的“面向对象技术的高峰会议”(OOPSLA)上发表的一篇文章《数据抽象和层次》(Data Abstraction and Hierarchy)中提出:继承必须确保超类拥有的性质在子类中仍然成立(Inheritance should ensure that any property proved about supertype objects also holds for subtype objects)。也就是说:当一个子类的实例能够替换任何父类的实例时,它们之间才具有is-A关系。
里氏替换原则主要阐述了有关继承的一些原则,即什么时候应该使用继承,什么时候不应该使用继承,以及其中蕴含的原理。里氏替换原则是继承复用的基础,它反映了基类与子类之间的关系,是开闭原则的补充,是实现抽象化的具体步骤的规范。
里氏替换原则通俗的讲就是:子类可以拓展父类的功能,但不能改变父类原有的功能。也就是说:由父类派生出子类时,除了添加新的方法完成新增功能外,尽量不要重写父类的方法。
如果通过重写父类的方法来完成新的功能,这样写起来虽然简单,但是整个继承体系的可复用性会比较差,特别是运用多态比较频繁时,程序运行出错的概率会很大。
边栏推荐
- [unity][ecs] learning notes (III)
- [Unity]内置渲染管线转URP
- Idea failed to connect to SQL Sever
- appliedzkp zkevm(10)中的Transactions Proof
- Minimum stack < difficulty coefficient >
- 【实战】1364- 实现一个完美的移动端瀑布流组件(附源码)
- DataEase安装升级
- 2022吴恩达机器学习specialization Week 2 practice lab: Linear regression
- The boss asked me to write an app automation -- yaml file reading -- with the whole framework source code attached
- fastposter v2.8.4 发布 电商海报生成器
猜你喜欢

Idea failed to connect to SQL Sever

Yann LeCun新论文:构建自动智能体之路
![[Unity]EBUSY: resource busy or locked](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[Unity]EBUSY: resource busy or locked

Realization of a springboard machine

【力扣——动态规划】整理题目1:基础题目:509、70、746、62、63、343、96(附链接、题目描述、解题方法及代码)

Mysql通用二进制安装方式

移动命令
![[NLP] this year's college entrance examination English AI score is 134. The research of Fudan Wuda alumni is interesting](/img/a8/51b95432a9c8f25d8440cfd80926ce.png)
[NLP] this year's college entrance examination English AI score is 134. The research of Fudan Wuda alumni is interesting

使用 ABAP 操作 Excel 的几种方法

How to use output in katalon
随机推荐
Summary of characteristics of five wireless transmission protocols of Internet of things
Does flink1.15 support MySQL views? I configured the view name at the table name to save, but the table could not be found. Think
appliedzkp zkevm(10)中的Transactions Proof
Pop up and push in sequence of stack < difficulty coefficient >
Katalon全局变量在TestObject引用
Katalon当中的output使用方法
metersphere使用js刷新当前页面
Fastposter v2.8.4 release e-commerce poster generator
BLE蓝牙模块NRF518/NRF281/NRF528/NRF284芯片方案对比
MySQL (I)
[QT] connect syntax reference implementation
广州海关支持保障食品、农产品和中药材等民生物资稳定供港
物联网无线通信应用中6种融合定位技术
DlhSoft Kanban Library for WPF
MySQL(一)
Internet of things application case of wireless module transparent transmission technology
What is the best way to learn machine learning
AGCO AI frontier promotion (6.28)
2022 Wu Enda machine learning specialization week 2 practice lab: linear expression
Ribbon核心源码解析