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
Developer articles
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 }}
microwind
design-patterns
Repository navigation
Code
Issues
Pull requests
41
(41)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
design-patterns
/
programming-paradigm
/
object-oriented-programming
/
interface
/
InterfaceExample.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
62 lines (50 loc) · 2.12 KB
main
Breadcrumbs
design-patterns
/
programming-paradigm
/
object-oriented-programming
/
interface
/
InterfaceExample.js
Copy path
Top
File metadata and controls
Code
Blame
62 lines (50 loc) · 2.12 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
接口是一种抽象方法的集合,与类属于不同的概念。类描述对象的属性和方法,接口则定义类需要实现的方法。
接口和抽象类有点像,都是一种抽象类型,但两者很大不同。
抽象类是对一种事物整体的抽象,即对类抽象,包括属性、行为等,子类和抽象类是一种"is-a"的关系。
而接口是对行为的抽象,是对类局部(行为)进行抽象,接口表示一种"can-do"的关系。
抽象类作为子类的父类,它是一种模板式设计。接口是一种行为规范,它是一种辐射式设计。
JS中没有interface,可以用class模拟。
本示例:
- Door接口定义了门的基本行为:打开和关闭。
- Alarm接口定义了报警器的行为:触发报警。
- AbstractAlarm抽象类实现了Alarm接口,提供了一个空的trigger()方法,具体的报警器类可以通过覆盖这个方法来实现具体的报警逻辑。
- SecurityDoor类实现了Door接口,并继承了AbstractAlarm类,表示一种既可以打开和关闭的门,又具有报警功能。
通过这种设计,我们实现了门和报警器的分离,并且可以轻松地扩展系统以支持其他类型的门和报警器,而不需要修改现有的代码。
*/
// 定义门接口
class Door {
open() {} // 打开门
close() {} // 关闭门
}
// 定义报警器接口
class Alarm {
trigger() {} // 触发报警
}
// 抽象类:AbstractAlarm,实现了报警器接口
class AbstractAlarm extends Alarm {
trigger() {
// 空方法体,具体报警器实现时需覆盖该方法
}
}
// 实现安全门类,继承报警抽象类,同时实现了门接口和报警器接口
class SecurityDoor extends AbstractAlarm {
open() {
console.log("安全门打开");
}
close() {
console.log("安全门关闭");
}
// 覆盖父类的trigger方法,实现具体的报警逻辑
trigger() {
console.log("警报触发! 有一条安全告警。");
}
}
// 测试验证
let securityDoor = new SecurityDoor();
// 打开门
securityDoor.open();
// 关闭门
securityDoor.close();
// 触发报警
securityDoor.trigger();
You can’t perform that action at this time.