本文实例讲述了javascript使用prototype原型实现的封装继承多态。分享给大家供大家参考,具体如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>www.jb51.net js基于原型prototype封装继承多态</title> </head> <body> <script> function person(name,age) { this.name=name; this.age=age; } person.prototype.getname=function() { return this.name; } person.prototype.setname=function(name) { this.name=name; } person.prototype.getage=function() { return this.age; } person.prototype.setage=function(age) { this.age=age; } //-------------------- function student(name,age,c) { this.name=name; this.age=age; this.c=c; } student.prototype=new person(this.name,this.age);//重点 student.prototype.getc=function() { return this.c; } student.prototype.setc=function(c) { this.c=c; } var a=new person("小小",19); document.write(a.getname()); //----------------------------------------- var b=new student("大大",15,1); document.write(b.getname()); </script> </body> </html>
运行结果:
小小大大
感兴趣的朋友可以使用在线html/css/javascript代码运行工具:测试一下上述代码。
更多关于javascript相关内容还可查看本站专题:《javascript面向对象入门教程》、《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》、《javascript遍历算法与技巧总结》及《javascript数学运算用法总结》
希望本文所述对大家javascript程序设计有所帮助。
您可能感兴趣的文章:
如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!
网友评论