面向对象--(继承)

实现继承的关键在于:子类必须拥有父类的全部属性和方法,同时子类还应该能定义自己特有的属性和方法

父类:

function People(name,age,sex){
				this.name=name;
				this.age=age;
				this.sex=sex;
			}
			People.prototype.sayHello=function(){
				console.log('你好,我是'+this.name+'我今年'+this.age+'岁了')
			}
			People.prototype.sleep=function(){
				console.log(this.name+'开始睡觉~~~')
			}

子类:

function Student(name,age,sex,school,studentNumber){
				this.name=name;
				this.age=age;
				this.sex=sex;
				this.school=school;
				this.studentNumber=studentNumber
			}
			
			//关键语句,实现继承
			Student.prototype=new People();
			
			Student.prototype.study=function(){
				console.log(this.name+'正在学习')
			}
			Student.prototype.exam=function(){
				console.log(this.name+'正在考试,加油')
			}
			
			//实例化
			var hanmeimei=new Student('韩梅梅',9,'女','清华小学','100556')
			hanmeimei.study()
			hanmeimei.sayHello()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值