Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
finalxcode
Design-Patterns
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Design-Patterns
/
SubClass.php
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
44 lines (36 loc) · 1.87 KB
master
Breadcrumbs
Design-Patterns
/
SubClass.php
Copy path
Top
File metadata and controls
Code
Blame
44 lines (36 loc) · 1.87 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace DesignPatterns;
class SubClass extends SuperClass
{
/**
* 两数相减
* @param int $num1
* @param int $num2
* @return int
*/
public function add($num1, $num2) {
return $num1 - $num2;
}
public function addV2($num1, $num2, $num3 = 0) {
return $num1 + $num2 + $num3;
}
public function addV3($num1, $num2) {
return [$num1 + $num2, $num1 - $num2];
}
}
//父类定义的add方法本来是计算两个数之和的,但是子类继承父类之后该方法却变成了两数相减
//这样当你把父类的方法替换成子类时,就会发现正常的功能现在出现问题了
//为了规范继承关系,解决此问题,里氏替换原则第一点规定:
//子类可以实现父类中的抽象方法,但是不能重写父类中的非抽象方法。
//但是这个时候,如果我们迫不得已需要在子类中添加一个跟父类方法名称相同的方法时
//应该怎么办呢?里氏交换方法规定:
//当子类需要重写父类的方法时,子类的形参要比父类方法中的参数要更宽松
//比如我们这样定义addV2这样的方法,在子类替换父类的时候就不会出问题
//对于强语言来说,我们可以在子类的方法定义中定义形参的类型更加宽泛些
//除了参数之外,里氏替换原则,还规定:
//重写父类的方法时,方法的返回值可以被缩小,但是不能被放大
//这一点可能难以理解,在php中没有规定方法的返回值类型,按照上面的定义,
//那么在子类重写父类方法的返回值,应该是父类方法返回值的子级,而不应该是父级
//这样在子类替换父类的时候就不会出现返回值类型超过了承受的范围。
//里氏替换原则的最后一点:
//子类可以拥有自己独特的方法或者属性,这一点没有什么要说的。
You can’t perform that action at this time.