javascript面向对象特征

<span style="font-size:18px;"><!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.stu
this.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></span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值