javascript面向对象特征

<!doctype html><html><head><meta charset="utf-8"><title>无标题文档</title></head><script type="text/javascript">document.write("js面向对象:封装");function Person(name,sal){this.name=name; //公开var sal=sal;//私有this.showInfo=function(){ //公开document.write("<br/>"+this.name+"–"+sal);}}var per=new Person("tom",100);per.showInfo();document.write("<br/>js面向对象:继承");//1.抽取共有属性,,写一个父类function Stu(name,age){this.name=name;this.age=age;this.show=function(){document.write("<br/>"+this.name+"–"+this.age);}}function MidStu(name,age){//这里相当于把Stu构造函数(类)赋值给我们的属性this.stuthis.stu=Stu;//这个表示初始化MidStu,相当于执行Stu(name,age),这句话必须有,否则无法实现集成的效果this.stu(name,age);//可以写自己的函数this.pay=function(fee){document.write("<br/>学费是:"+fee*0.8);}}var midstu=new MidStu("小白",15);midstu.show();midstu.pay(100);document.write("<br/>js面向对象:重载");//js通过判断参数的个数来实现重载function Person1(){this.test1=function (){if(arguments.length==1){this.show1(arguments[0]);}else if(arguments.length==2){this.show2(arguments[0],arguments[1]);}else if(arguments.length==3){this.show3(arguments[0],arguments[1],arguments[2]);}}this.show1=function(a){document.write("show1()被调用"+a);}this.show2=function(a,b){document.write("show2()被调用"+"–"+a+"–"+b);}function show3(a,b,c){document.write("show3()被调用");}}var p1=new Person1();//js中不支持重载.//p1.test1("a","b","c");p1.test1("a","b");p1.test1("a");document.write("<br/>js面向对象:覆盖");function Fu(){this.fu=function(){document.write("<br/>父类的方法");}}function Zi(){this.stu=Fu;this.stu();this.fu=function(){document.write("<br/>子类的方法");}}var zi=new Zi();zi.fu();document.write("<br/>js面向对象:多态");// Master类function Master(name){this.nam=name;//方法[给动物喂食物]}//原型法添加成员函数Master.prototype.feed=function (animal,food){document.write("<br>给"+animal.name+" 喂"+ food.name);}function Food(name){this.name=name;}//鱼类function Fish(name){this.food=Food;this.food(name);}//骨头function Bone(name){this.food=Food;this.food(name);}//桃子function Peach(name){this.food=Food;this.food(name);}//动物类function Animal(name){this.name=name;}//猫猫function Cat(name){this.animal=Animal;this.animal(name);}//狗狗function Dog(name){this.animal=Animal;this.animal(name);}//猴子function Monkey(name){this.animal=Animal;this.animal(name);}var cat=new Cat("大花猫");var fish=new Fish("黄花鱼");var dog=new Dog("大花狗");var bone=new Bone("猪骨头");//创建一个主人var master=new Master("韩顺平");master.feed(dog,bone);//扩展var monkey=new Monkey("金丝猴");var peach=new Peach("仙桃");master.feed(monkey,peach);</script><body></body></html>

版权声明:博主原创文章,转载请说明出处。

既有美妙的风景,也会有称不上景只有风的地方。

javascript面向对象特征

相关文章:

你感兴趣的文章:

标签云: