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
/
polymorphism
/
AnimalExample.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
71 lines (62 loc) · 1.77 KB
main
Breadcrumbs
design-patterns
/
programming-paradigm
/
object-oriented-programming
/
polymorphism
/
AnimalExample.js
Copy path
Top
File metadata and controls
Code
Blame
71 lines (62 loc) · 1.77 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
63
64
65
66
67
68
69
70
71
/* JS实现多态例子 */
// 定义一个通用Animal类
class Animal {
makeSound() {
console.log("Animal makes a sound");
}
}
// 定义Dog类,它是动物的子类
class Dog extends Animal {
makeSound() {
console.log("Dog barks");
}
}
// 定义Cat类,它是动物的子类
class Cat extends Animal {
makeSound() {
console.log("Cat meows");
}
// Cat自有函数
meow() {
console.log(this.constructor.name + " is meowing...", this);
}
}
// 定义Bird类,也继承自Animal
class Bird extends Animal {
makeSound() {
console.log("Bird chirps");
}
}
// 定义一个动物园类,管理不同的动物
class Zoo {
// JS没有严格类型,除原始数据类型外,其他均是Object。
// 参数无需类型声明,传入的对象只要有makeSound方法即可。
letAnimalMakeSound(animal) {
animal.makeSound();
}
}
// 测试代码
const zoo = new Zoo();
// JS没有父类定义子类概念,直接声明即可,无需向上转型
// 通过instanceof类型判断时可得到子类和父类类型
const myDog = new Dog();
const myCat = new Cat();
const myBird = new Bird();
// 直接调用自有函数
myCat.meow();
// 可以动态给对象设置函数并绑定对象
myDog.meow = myCat.meow.bind(myDog);
myDog.meow();
// 动物园可以使用相同的方法处理不同种类的动物
// 当需要增加其他动物时,直接建立新的类继承Animal,而无需修改Zoo。
zoo.letAnimalMakeSound(myDog); // 输出 "Dog barks"
zoo.letAnimalMakeSound(myCat); // 输出 "Cat meows"
zoo.letAnimalMakeSound(myBird); // 输出 "Bird chirps"
/*
jarry@jarrys-MacBook-Pro polymorphism % node AnimalExample.js
Cat is meowing... Cat {}
Cat is meowing... Dog { meow: [Function: bound meow] }
Dog barks
Cat meows
Bird chirps
*/
You can’t perform that action at this time.