function Animal(){
this.head="头";
this.foot="脚";
this.name="动物";
this.birthday=new Date();
this.getAge=function(){
return new Date()-this.birthday;
}
}
function Bird(name){
Animal.apply(this);//类似于Java的super(),父类如果有参数,则为super(args),即Animal.apply(this,args);
this.name=name;
}
function Dog(name){
Animal.call(this);//类似于Java的super(),父类如果有参数,则为super(args),即Animal.call(this,arg1,arg2,arg3);
this.name=name;
this.hand="手";
}
var bird=new Bird("鸟");
bird.name;//鸟
bird.hand;
bird.getAge();
var dog=new Dog("狗");
dog.name;//狗
dog.hand;
dog.getAge();
var animal = new Animal();
animal.name;//动物
Javascript继承的概念理解
最新推荐文章于 2022-01-09 23:28:53 发布