您的位置:首页 > 编程语言 > Java开发

重载不是多态的体现形式

2015-09-02 11:44 232 查看
下面这段文字来字《Java In Thinking 3》

Polymorphism means “different forms.”In object-oriented programming, you have the same face(the common interface in the base class) and different forms useing the face: the different versions of the dynamically bound methods.

You've seen in this chapter that it's impossible to understand , or even create, an example of polymorphism without using data abstraction and inheritance. Polymorphism is a feature that cannot be viewed in isolation(like a switch statement can ,for example), but instead works only in concert, as part of a "big picture" of class relationships,People are often  confused by other, non-object-oriented features of Java, like method overloding, which are sometimes presented as object-oriented. Don't be fool:if it isn't late binding, it isn't polymorphism.

To use polymorphism-and thus object-oriented techniques-effectively in your programs you must expand your view of programming to include not just members and messages of an individual class, but also the commonality among classes and their relationships with each other. Although this requires significant effort, it's a worthy struggle, because the results are faster program development, better code organization, extensible programs, and easier code maintenance.


多态意味着“多种形态”。在面向对象的编程当中,你有相同的’脸’(基类里一种通用的接口),以及使用该接口的不同的形式:即各种不同的动态绑定方法的版本。

在本章中介绍过,如果没有理解数据抽象(封装)和继承,是不可能理解或者创建一个多态的例子,多态是一个不能被孤立看待的特征(但是像switch就可以),相反,在类的大的环境里面,多态作为一种很广泛的应用,人们经常被Java中其它非面向对象的特征所困扰,比如方法重载有时会被当作一个面向对象的特征,别被骗了,如果不是晚绑定,它就不是多态。

要想在程序中高效的使用多态及面向对象的技术,你必须将你的编程的视角不能仅限定在某一个类的成员和信息,还应该考虑到类与其他类之间通用的关系 ,虽然这个技术需要花费不少时间 ,但它是值得努力的,因为他可以更快的开发应用程序,能带来更好的代码组织,程序的可扩展性,以及更好的代码可维护性。

总之:方法重写是可以决定多态的,方法重载是决定不了多态的

所以在Java中,“多态体现在方法重载与方法重写”,这句话还正确么?

ps:

早绑定:出现在编译期;

晚绑定:出现在运行期.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息