過載不是多型的體現形式
阿新 • • 發佈:2019-02-12
下面這段文字來字《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:
早繫結:出現在編譯期;
晚繫結:出現在執行期.