当前位置:网站首页>方法重写(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关系。
里氏替换原则主要阐述了有关继承的一些原则,即什么时候应该使用继承,什么时候不应该使用继承,以及其中蕴含的原理。里氏替换原则是继承复用的基础,它反映了基类与子类之间的关系,是开闭原则的补充,是实现抽象化的具体步骤的规范。
里氏替换原则通俗的讲就是:子类可以拓展父类的功能,但不能改变父类原有的功能。也就是说:由父类派生出子类时,除了添加新的方法完成新增功能外,尽量不要重写父类的方法。
如果通过重写父类的方法来完成新的功能,这样写起来虽然简单,但是整个继承体系的可复用性会比较差,特别是运用多态比较频繁时,程序运行出错的概率会很大。
边栏推荐
猜你喜欢
随机推荐
File的io流与base64
Spatial-Temporal时间序列预测建模方法汇总
Resolution: overview of decentralized hosting solution
【monkey】monkey测试入门
Training and recognition of handwritten digits through the lenet-5 network built by pytorch
Summary of spatial temporal time series prediction modeling methods
Transactions proof in appliedzkp zkevm (10)
Several methods of using ABAP to operate Excel
Fabric. How to use js brush?
[Unity][ECS]学习笔记(一)
metersphere使用js刷新当前页面
An idea plug-in that automatically generates unit tests, which improves the development efficiency by more than 70%!
如何使用 DataAnt 监控 Apache APISIX
【LeetCode每日一题】【2021/12/19】997. 找到小镇的法官
How to use output in katalon
数据库系列:有什么办法对数据库的业务表进行无缝升级
MySQL common commands for viewing database performance
Information hidden in the trend chart of Hong Kong London gold market
The introduction of flink-sql-mysql-cdc-2.2.1 has solved many dependency conflicts?
Bytecode proof in appliedzkp zkevm (9)







