惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

博客园 - newbin

<该停暖气了> stay hungry,stay foolish Stay hungry, Stay foolish... string与stringbuilder的区别 为了实现自我价值,加油,加油! abstract和interface的异同 [转贴]一名大学生应聘软件工程师的经历和感受 c# brief datagrid,datalist,repeater在Html页中的定义 关于const的一些解释 系统默认提供的CSS样式风格定义 thanks giving 真不知道怎么过 找根火柴棍把眼皮支起来 软件开发的四项基本原则 我们为什么需要 XML Windows 2000的引导过程 Understand CSS Terminology ROUNDTRIP AND POSTBACK 生命如此年轻
多态和继承(Inheritance)
newbin · 2005-02-22 · via 博客园 - newbin

严格来说,多态与继承、重载并不是孤立的,他们之间存在着紧密的联系,多态是建立在这两者的基础之上的(实际上继承就有用重载这一特性)。


  传统的多态实际上就是由虚函数(Virtual Function)利用虚表(Virtual Table)实现的(早期C模拟OO特性时使用最多,C++的实现也是,后来的技术未作研究,是否使用VT不得而知),自然是离不开继承,换句话说多态实际上覆盖了继承。

  正是由于继承与多态的紧密联系,使得我们很容易张冠李戴,那么如何区别呢?

  举个常用的例子:

Abstract Class Sharp 
{
 
public bool isSharp()
 
{
   
return true;
 }

 
public abstract int getSides();
}


Class Triangle : Sharp 
{
 
public override int getSides()
 
{
   
return 3;
 }

}


Class Rectangle : Sharp 
{
 pubilc 
override int getSides()
 
{
  
return 4;
 }

}


那么这种类的关系叫做继承,下面这种使用方式也是继承所带来的:

Triangel tri = new Triangle();
println("Triangle is a type of sharp? " + tri.isSharp());

而这种方式则是多态:
Sharp sharp = new Rectangle();
println("My sharp has " + sharp.getSides() + " sides.");

这两者区别在哪?很显然,继承是子类使用父类的方法,而多态则是父类使用子类的方法。

其技术上的区别是绑定时期,晚期绑定一定是多态。