Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
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.