构造方法:

构造方法:

		class Student{
			constructor (name,age){
				this.name 

=name;
				this.age=age;
			}
			run(){
				console.log("我会跑");
			}
		}
		let xs = new Student("曹伟",22);
		console.log(xs);//打印:Student {name: "曹伟", age: 22}。
		//constructor:实例化那些默认属性。
		
		
		继承:
		class Teacher extends Student{
			constructor (name,age,sex){
				super(name,age);
				this.sex=sex;
			}
			eat(){
				console.log(this.name 

+"is eating")
			}
		}
		var ls = new Teacher("美女","30","母");
		console.log(ls);//打印:Teacher {name: "美女", age: "30", sex: "母"}。
		ls.eat();//打印:老师is eating。
		//extends:继承。
		//super:继承属性方法。
		注释:在构造方法里的super是指反类的构造方法。

		get,set,static:
		class Student{
			constructor (name,age){
				this.name 

=name;
				this.age=age;
			}
			run(){
				console.log("我会跑");
			}
			get xm(){
				return this.name 

+"123";
			}
			set xm(value){
				this.name 

=value;
			}
			static shangxue (){
				console.log("去读书");
			}
		}
		let xs = new Student("曹伟",25);
		console.log(xs.xm);
		xs.xm="骚胖";
		console.log(xs.xm);
		Student.shangxue();
		//get:获取加赋值。
		//set:设置。
		//static:静态方法|类方法。
		//set和get的方法名相同,而且可以同名
		
		
		方法重载|方法覆盖:
		class Student{
			constructor (name,age){
				this.name 

=name;
				this.age=age;
			}
			run(){
				console.log("我会跑");
			}
		}
		let xs = new Student("曹伟",25);
		
		class Teacher extends Student{
			constructor (name,age,sex){
				super(name,age);
				this.sex=sex;
			}
			eat(){
				console.log(this.name 

+"is eating")
			}
run(){
			super.run();
			console.log("我一直在跑");
			}
		}
		var ls = new Teacher("老师","30","男");
		ls.run();//我会跑 我一直在跑;
		
		注释:虽然子类继承了父类的run方法,但是子类会把父类的方法给覆盖掉,这个就是方法覆盖。
posted @ 2017-07-17 20:27  前端兵哥哥  阅读(194)  评论(0编辑  收藏  举报