以 JavaScript 實作多型 (Polymorphism)
JavaScript簡介歷史開發工具基本語法運算式分枝迴圈函數陣列物件導向原型封裝繼承多型this控制流程進階功能Eval 函數ClosureJSONP小書籤字串正規表達式除錯方法伺服端播 midicookie套件ccc函式庫2D 繪圖3D 繪圖影像處理訊號處理語音處理數學計算tex 數學式格式轉換桌面應用自然語言地理資訊平台Node.jsjQuerynumeric.jsTitanium引擎語法作品翻譯精靈繪圖精靈DotWiki流程前端工程師後端工程師css訊息相關網站參考文獻最新修改簡體版English |
程式範例<html> <body> <script type="text/javascript"> function Person(name, age) { this.name = name; this.age = age; this.toStr = function() { return "Person:name="+this.name+" age="+this.age; } } var john = new Person("John", 40); function Student(name, age, grade) { this.prototype = Person; this.prototype(name, age); this.grade = grade; this.toStr = function() { return "Student:name="+this.name+" age="+this.age+" grade="+this.grade; } } var tony = new Student("Tony", 19, "Freshman"); var array = [ john, tony ]; for (i in array) document.write("array["+i+"].toStr()="+array[i].toStr()+"<BR/>"); </script> </body> </html> 執行結果
範例網頁的顯示結果:http://dl.dropbox.com/u/6387819/JS/Polymorphism.htm |
page revision: 2, last edited: 07 Sep 2010 09:11
Post preview:
Close preview