Javascript class定义和继承使用

2018-01-06 17:48:00
admin
原创
516
<!DOCTYPE html>
<html>
<head>
    <title>Javascript class定义和继承使用</title>
    <script type="text/javascript">
class Student {

 constructor(name) {

   this.name = name;

 }


 hello() {

  alert('Hello, ' + this.name + '!');

 }

} 
		
class PrimaryStudent extends Student {

 constructor(name, grade) {

   super(name); // 记得用super调用父类的构造方法!

   this.grade = grade;

 }


 myGrade() {

 alert('I am at grade ' + this.grade);

 }
 hello(){
	  alert('Override Hello, ' + this.name + '!');
 }

} 


		var s1 = new Student("TOm");
		s1.hello();
		var p1 = new PrimaryStudent("Tom2","grade1");
		p1.hello();
		p1.myGrade();
        
    </script>
</head>
<body>

</body>
</html>				
				
  • QQ讨论群
  • 关注微信
    • 欢迎咨询
    • 欢迎加入QQ群 16506021
  • 公众号:源宝网络