先声明一个Person类作为父类:
function
Person(name,age)
...
{
this.name = name;
this.age = age;
if (typeof(Person._initialized) == "undefined")...{
Person.prototype.EatFood = function()...{
alert(this.nae + "is eating rice...");
};
}
this.name = name;
this.age = age;
if (typeof(Person._initialized) == "undefined")...{
Person.prototype.EatFood = function()...{
alert(this.nae + "is eating rice...");
};
}
Person._initialized = true;
}
}
让后声明一个Teacher类和一个Student类,这两个类继承Person类
//
老师类
function Teacher(name,age,course) ... {
Person.call(this,name,age);
this.course = course;
if(typeof(Teacher._initialized) == "undefined")...{
Teacher.prototype.Teach = function()...{
alert(this.name + " is teach " + this.course + "...");
};
}
Teacher._initialized = true;
}
Teacher.prototype = new Person();
// 学生类
function Student(name,age,stuId) ... {
Person.call(this,name,age);
this.stuId = stuId;
if (typeof(Student._initialized) == "undefined")
...{
Student.prototype.Study = function()...{
alert(this.name + " is studing ...");
};
}
Student._initialized = true;
}
Student.prototype = new Person();
function Teacher(name,age,course) ... {
Person.call(this,name,age);
this.course = course;
if(typeof(Teacher._initialized) == "undefined")...{
Teacher.prototype.Teach = function()...{
alert(this.name + " is teach " + this.course + "...");
};
}
Teacher._initialized = true;
}
Teacher.prototype = new Person();
// 学生类
function Student(name,age,stuId) ... {
Person.call(this,name,age);
this.stuId = stuId;
if (typeof(Student._initialized) == "undefined")
...{
Student.prototype.Study = function()...{
alert(this.name + " is studing ...");
};
}
Student._initialized = true;
}
Student.prototype = new Person();