首页 > 技术文章 > 创建继承person的student子类

304979850w 2020-06-22 17:41 原文

创建构造函数Person,添加属性姓名(name)、语文成绩(chinese)、数学成绩(math);

添加三个方法,分别返回姓名、语文和数学成绩

创建构函数Student,继承Person的属性和方法,并添加属于自己的属性年龄(age),

添加属于自己的方法,返回年龄
创建Student的对象,并在页面上输出实例的姓名、语文、数学成绩和年龄

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>创建继承person的student子类</title>
</head>
<body>
<p>姓名:<span id="name"></span></p>
<p>语文:<span id="chinese"></span></p>
<p>数学:<span id="math"></span></p>
<p>年龄:<span id="age"></span></p>
<script>
function Person(name,chinese,math){
this.name=name;
this.chinese=chinese;
this.math=math;
}
Person.prototype.showName=function(){
return this.name;
};
Person.prototype.showChinese=function(){
return this.chinese;
};
Person.prototype.showMath=function(){
return this.math;
};
function Student(name,chinese,math,age){
Person.call(this,name,chinese,math);
this.age=age;
}
Student.prototype=new Person();
Student.prototype.showAge=function(){
return this.age;
};
var student1=new Student("浩然",100,107,108);
document.getElementById("name").innerHTML=student1.showName();
document.getElementById("chinese").innerHTML=student1.showChinese();
document.getElementById("math").innerHTML=student1.showMath();
document.getElementById("age").innerHTML=student1.showAge();
</script>
</body>
</html>

 

推荐阅读