在现代程序语言中,源代码可以是以书籍或者磁带的形式出现,但最为常用的格式是文本文件,这种典型格式的目的是为了编译出计算机程序。计算机源代码的最终目的是将人类可读的文本翻译成为计算机可以执行的二进制指令,这种过程叫做编译,通过编译器完成。
functionAnimal(){}
Animal.prototype={
name:"animal",
toString:function(){
console.log(this.name);
}
};
Animal.prototype.constructor=Animal;
functionDog(){}
//用于打破对象的引用传递,防止修改子类属性对父类产生影响
varF=function(){}
F.prototype=Animal.prototype
Dog.prototype=newF();
Dog.prototype.constructor=Dog;
Dog.prototype.name="dog";
vard=newDog();
d.toString();//打印子类namedog
vara=newAnimal();
a.toString();//打印父类nameanimal
上面代码通过实例化子类和父类,分别调用toString()实现了继承的关系。
这个时候有这样的需求;不实例化父类,直接通过子类完完整整的调用父类的方法或属性。
实现代码如下
functionAnimal(){}
Animal.prototype={
name:"animal",
toString:function(){
console.log(this.name);
}
};
Animal.prototype.constructor=Animal;
functionDog(){}
//用于打破对象的引用传递,防止修改子类属性对父类产生影响
varF=function(){}
F.prototype=Animal.prototype
Dog.prototype=newF();
Dog.prototype.constructor=Dog;
Dog.prototype.name="dog";
Dog.uber=Animal.prototype;
vard=newDog();
d.toString();//打印子类namedog
//vara=newAnimal();
//a.toString();//打印父类nameanimal
/**
*Dog.prototype.constructor===d.constructor
*/
Dog.prototype.constructor.uber.toString();//打印animal(方式1)
d.constructor.uber.toString();//打印animal(方式2)
通过面简单的三行红色代码就实现了子类访问父类成员的需求。
本来想模仿java的使用super访问父类,后来想想super是Javascript的关键字。
对软件进行说明,即对软件的编写进行说明。为数不少的初学者,甚至少数有经验的程序员都忽视软件说明的编写,因为这部分虽然不会在生成的程序中直接显示,也不参与编译。但是说明对软件的学习、分享、维护和软件复用都有巨大的好处。因此,书写软件说明在业界被认为是能创造优秀程序的良好习惯,一些公司也硬性规定必须书写。
¥99.00
¥59.00
¥39.00