OOP基本概念之讨论:重载也是实现多态的一种方法吗?
2006-03-07 13:38
901 查看
多态(Polymorphism)是OOP的一个最重要和最基本的概念之一。不同的面向对象程序设计语言都有不同的实现多态的方法。最基本的方法就是inheritance加override了,当然还有其它的一些方法。
现在有一个问题:overload是否也是实现多态的方法之一?
Thinking in C++中认为:多态的实现方法只有一个,也就是virtual function。
而也有其它的一些人认为,实现多态的方法很多。你认为呢?
以下来自于国外一个Blog的讨论,也说明overload是否多态是个问题。大家一起讨论讨论。
链接来自于:http://www.scottcreynolds.com/
Polymorphism is the one that always gave me trouble when I was learning OOP because it sounds a hell of a lot more complicated than it is at root, but also because there is just so much to polymorphism once you get deeper and deeper into OOP. At root, polymorphism means that one thing can have many shapes (or, in World of Warcraft, Polymorph is where a mage turns you into a sheep). From Wikipedia:
This definition is a little incomplete in my opinion. I am going to borrow a line from Raymond Lewallen (who did a great series on the 4 principles of OOP a while back) where he says that while polymorphism can be coupled with inheritance (as is implied by the wikipedia definition above) it does not require inheritance. You can see polymorphism at work in one class in the form of overloading. You can see polymorphism at work when implementing interfaces (covered later) that allows you to treat one object type like another if they share common interfaces. You can see polymorphism at work in inheritance via overriding inherited behaviors. Generics available in C# 2.0 are a type of polymorphism at work (called parametric polymorphism). A lot of that is way beyond the scope here. I only want to discuss overloading and overriding here, because those are the least complicated and the most often encountered when you first begin OOP and this is just an overview article. Polymorphism is one of those things that you can talk about for days and still not see the end of. Because of the many places you can see it at work, it’s one of the hardest parts of OO to define, and to fully understand, until you see it from all its different sides.
Overloading is a type of polymorphism (sometimes called ad-hoc polymorphism) where you essentially have multiple functions that perform the same task, return the same thing, and have the same name. They differ in the number and/or type of arguments passed in. Some will say that method overloading is not polymorphism. Others will say it is. I’m not here to deal in academics. In the real world, overloaded behavior is considered to be polymorphic behavior, at least in the sense that from the perspective of the consumer of the API it’s polymorphic, but maybe in the perspective of the author it’s just syntactic sugar for multiple methods. Overloading is important to understand and is a key feature of OOP, and I’m putting it under polymorphism. Deal. If you know enough about it to argue with me about whether or not it goes here…this post ain’t for you bub. :P
现在有一个问题:overload是否也是实现多态的方法之一?
Thinking in C++中认为:多态的实现方法只有一个,也就是virtual function。
而也有其它的一些人认为,实现多态的方法很多。你认为呢?
以下来自于国外一个Blog的讨论,也说明overload是否多态是个问题。大家一起讨论讨论。
链接来自于:http://www.scottcreynolds.com/
Polymorphism is the one that always gave me trouble when I was learning OOP because it sounds a hell of a lot more complicated than it is at root, but also because there is just so much to polymorphism once you get deeper and deeper into OOP. At root, polymorphism means that one thing can have many shapes (or, in World of Warcraft, Polymorph is where a mage turns you into a sheep). From Wikipedia:
polymorphism is behavior that varies depending on the class in which the behavior is invoked. For example, the result of bark() for a Dog would differ from the result of bark() for a Jackal; and in a more sophisticated animal-emulation program, bark() would differ for a Chihuahua and a Saint Bernard.
This definition is a little incomplete in my opinion. I am going to borrow a line from Raymond Lewallen (who did a great series on the 4 principles of OOP a while back) where he says that while polymorphism can be coupled with inheritance (as is implied by the wikipedia definition above) it does not require inheritance. You can see polymorphism at work in one class in the form of overloading. You can see polymorphism at work when implementing interfaces (covered later) that allows you to treat one object type like another if they share common interfaces. You can see polymorphism at work in inheritance via overriding inherited behaviors. Generics available in C# 2.0 are a type of polymorphism at work (called parametric polymorphism). A lot of that is way beyond the scope here. I only want to discuss overloading and overriding here, because those are the least complicated and the most often encountered when you first begin OOP and this is just an overview article. Polymorphism is one of those things that you can talk about for days and still not see the end of. Because of the many places you can see it at work, it’s one of the hardest parts of OO to define, and to fully understand, until you see it from all its different sides.
Overloading is a type of polymorphism (sometimes called ad-hoc polymorphism) where you essentially have multiple functions that perform the same task, return the same thing, and have the same name. They differ in the number and/or type of arguments passed in. Some will say that method overloading is not polymorphism. Others will say it is. I’m not here to deal in academics. In the real world, overloaded behavior is considered to be polymorphic behavior, at least in the sense that from the perspective of the consumer of the API it’s polymorphic, but maybe in the perspective of the author it’s just syntactic sugar for multiple methods. Overloading is important to understand and is a key feature of OOP, and I’m putting it under polymorphism. Deal. If you know enough about it to argue with me about whether or not it goes here…this post ain’t for you bub. :P
相关文章推荐
- JAV中一些基本概念:构造函数、方法重载、继承、方法重写、多态。
- 译:C#面向对象的基本概念 (Basic C# OOP Concept) 第三部分(多态,抽象类,虚方法,密封类,静态类,接口)
- 数据结构——线性表——散列存储结构——哈希表知识点总结 原创 2017年05月14日 10:08:40 散列(hashing)是一种重要的存储方法,也是一种常见的查找方法。 基本思想:以结点的
- java多态/重载方法——一个疑难代码引发的讨论
- 图形编程的基本概念即自定义view的实现方法
- A/B 测试的基本概念举例理解以及具体实现方法【传统A/B测试基于后端的 A/B 测试(Back-end AB test),现在基本上基于前端js在客户端进行分流,有更多优点,请看里面】
- 实现一个栈(元素遵守先入后出顺序),能够通过 min 方法在 O(1)时间内获取栈中的最小元素。同时,栈的基本操作:入栈(Push)、出栈(Pop),也是在O(1)时间内完成的
- 【Objective-C】OC中自定义对象的归档基本概念和使用方法(实现NSCoding协议)
- C语言实现多态的一种方法
- 【译文】 C#面向对象的基本概念 (Basic C# OOP Concept) 第一部分(类,对象,变量,方法,访问修饰符)
- 【Objective-C】OC中自定义对象的归档基本概念和使用方法(实现NSCoding协议)
- A/B 测试的基本概念举例理解以及具体实现方法【传统A/B测试基于后端的 A/B 测试(Back-end AB test),现在基本上基于前端js在客户端进行分流,有更多优点,请看里面】
- AOP基本概念、AOP底层实现原理、AOP经典应用【事务管理、异常日志处理、方法审计】
- C++ 封装、继承、多态、重载、覆盖、隐藏基本概念详解
- 面向对象,类的组合关系,继承,实现,方法重写,方法重载,this的使用,抽象方法和抽象类的比较,父类构造方法存在的意义,多态的是用和解析,各种访问修饰符
- 线程同步基本概念+实现线程同步的2种方法(无代码示例)
- AGPS定位基本流程,通过GSM来实现的AGPS,AGPS功能的一种实现方法
- (原创)底层最基本的也是用的最多的条件查询方法实现,有点多!(已经更新)
- 基本概念继承,封装,多态,重载
- 用 C# 做组件设计时的事件实现方法讨论