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 }}
cppg
toBeTopJavaer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
toBeTopJavaer
/
docs
/
basics
/
object-oriented
/
extends-implement.md
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
30 lines (16 loc) · 2.23 KB
master
Breadcrumbs
toBeTopJavaer
/
docs
/
basics
/
object-oriented
/
extends-implement.md
Copy path
Top
File metadata and controls
Code
Blame
30 lines (16 loc) · 2.23 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
前面的章节我们提到过面向对象有三个特征
:
封装
、
继承
、
多态
。
前面我们分别介绍过了这三个特性
。
我们知道
,
继承可以使用现有类的所有功能
,
并在无需重新编写原来的类的情况下对这些功能进行扩展
。
这种派生方式体现了
*
传递性
*。
在Java中
,
除了继承
,
还有一种体现传递性的方式叫做实现
。
那么
,
这两者方式有什么区别呢
?
继承和实现两者的明确定义和区别如下
:
继承
(
Inheritance
):
如果多个类的某个部分的功能相同
,
那么可以抽象出一个类出来
,
把他们的相同部分都放到父类里
,
让他们都继承这个类
。
实现
(
Implement
):
如果多个类处理的目标是一样的
,
但是处理的方法方式不同
,
那么就定义一个接口
,
也就是一个标准
,
让他们的实现这个接口
,
各自实现自己具体的处理方法来处理那个目标
继承指的是一个类
(
称为子类
、
子接口
)
继承另外的一个类
(
称为父类
、
父接口
)
的功能
,
并可以增加它自己的新功能的能力
。
所以
,
继承的根本原因是因为要
*
复用
*,
而实现的根本原因是需要定义一个
*
标准
*。
在Java中
,
继承使用
`
extends
`
关键字实现
,
而实现通过
`
implements
`
关键字
。
>
简单点说
,
就是同样是一台汽车
,
既可以是电动车
,
也可以是汽油车
,
也可以是油电混合的
,
只要实现不同的标准就行了
,
但是一台车只能属于一个品牌
,
一个厂商
。
```
class
Car
extends
Benz
implements
GasolineCar
,
ElectroCar
{
}
```
以上
,
我们定义了一辆汽车
,
他实现了电动车和汽油车两个标准
,
但是他属于奔驰这个品牌
。
像上面这样定义
,
我们可以最大程度的遵守标准
,
并且复用奔驰车所有已有的一些功能组件
。
另外
,
在接口中只能定义全局常量
(
static
final
)
和无实现的方法
(
Java
8
以后可以有default方法
);
而在继承中可以定义属性方法
,
变量
,
常量等
。
*
特别需要注意的是
,
Java中支持一个类同时实现多个接口
,
但是不支持同时继承多个类
。*
但是这个问题在Java
8
之后也不绝对了
。
关于多继承的问题
,
我们下一个章节中介绍
。
You can’t perform that action at this time.