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
Accelerator
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 }}
Uh oh!
There was an error while loading.
Please reload this page
.
Interview-Notes
/
toBeTopJavaer
Public
forked from
hollischuang/toBeTopJavaer
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
toBeTopJavaer
/
basics
/
java-basic
/
polymorphism.md
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
54 lines (36 loc) · 2.83 KB
master
Breadcrumbs
toBeTopJavaer
/
basics
/
java-basic
/
polymorphism.md
Copy path
Top
File metadata and controls
Code
Blame
54 lines (36 loc) · 2.83 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
###
什么是多态
,
多态有什么好处
,
多态的必要条件是什么
、
Java中多态的实现方式
多态的概念呢比较简单
,
就是同一操作作用于不同的对象
,
可以有不同的解释
,
产生不同的执行结果
。
如果按照这个概念来定义的话
,
那么多态应该是一种运行期的状态
。
为了实现运行期的多态
,
或者说是动态绑定
,
需要满足三个条件
。
即有类继承或者接口实现
、
子类要重写父类的方法
、
父类的引用指向子类的对象
。
简单来一段代码解释下
:
public
class
Parent
{
public
void
call
(){
sout
(
"im Parent"
);
}
}
public
class
Son
extends
Parent
{
// 1.有类继承或者接口实现
public
void
call
(){
// 2.子类要重写父类的方法
sout
(
"im Son"
);
}
}
public
class
Daughter
extends
Parent
{
// 1.有类继承或者接口实现
public
void
call
(){
// 2.子类要重写父类的方法
sout
(
"im Daughter"
);
}
}
public
class
Test
{
public
static
void
main
(
String
[]
args
){
Parent
p
=
new
Son
();
//3.父类的引用指向子类的对象
Parent
p1
=
new
Daughter
();
//3.父类的引用指向子类的对象
}
}
这样
,
就实现了多态
,
同样是Parent类的实例
,
p
.
call
调用的是Son类的实现
、
p1
.
call调用的是Daughter的实现
。
有人说
,
你自己定义的时候不就已经知道p是son
,
p1是Daughter了么
。
但是
,
有些时候你用到的对象并不都是自己声明的啊
。
比如Spring
中的IOC出来的对象
,
你在使用的时候就不知道他是谁
,
或者说你可以不用关心他是谁
。
根据具体情况而定
。
另外
,
还有一种说法
,
包括维基百科也说明
,
多态还分为动态多态和静态多态
。
上面提到的那种动态绑定认为是动态多态
,
因为只有在运行期才能知道真正调用的是哪个类的方法
。
还有一种静态多态
,
一般认为Java中的函数重载是一种静态多态
,
因为他需要在编译期决定具体调用哪个方法
、
关于这个动态静态的说法
,
我更偏向于重载和多态其实是无关的
。
但是也要看情况
,
普通场合
,
我会认为只有方法的重写算是多态
,
毕竟这是我的观点
。
但是如果在面试的时候
,
我
“
可能
”
会认为重载也算是多态
,
毕竟面试官也有他的观点
。
我会和面试官说
:
我认为
,
多态应该是一种运行期特性
,
Java中的重写是多态的体现
。
不过也有人提出重载是一种静态多态的想法
,
这个问题在StackOverflow等网站上有很多人讨论
,
但是并没有什么定论
。
我更加倾向于重载不是多态
。
这样沟通
,
既能体现出你了解的多
,
又能表现出你有自己的思维
,
不是那种别人说什么就是什么的
。
You can’t perform that action at this time.