面向对象语言特点:封装,继承,多态

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<title></title>

</head>

<body>

<script type="text/javascript">

/*

* 面向对象语言特点:封装,继承,多态

*/

function Person(name,gender,age){

this.name = name;

this.gender = gender;

this.age = age;

}

Person.prototype.eat = function(){

console.log("韩风源");

}

Person.prototype.drink = function(){

console.log("百威,不知道真假");

}

 

var per = new Person("光头强","男",18);

console.log(per.name,per.gender,per.age);

per.eat();

per.drink();

 

// function Student(name,gender,age,stu_id){

// this.name = name;

// this.gender= gender;

// this.age = age;

/*

* 对象1.apply(对象2,[参数一,参数二,...]):在此处的作用是完成继承操作

* apply的作用是将对象1对应的操作应用到对象2身上

*/

// Person.apply(this,[name,gender,age]);

/*

* 对象1.call(对象2,参数一,参数二,...):作用同apply只不过call方法的第二个参数不是数组

*/

// //Person.call(this,name,gender,age);

// //student的特有的属性

// this.stu_id = stu_id;

// }

// Student.prototype.eat = function(){

// console.log("兴达");

// }

// Student.prototype.drink = function(){

// console.log("鸡尾酒");

// }

// var stu = new Student("熊大","男",5,110);

// console.log(stu.name,stu.gender,stu.age,stu.stu_id);

// stu.eat();

// stu.drink();

// //_proto_获取当前对象的上一个参考对象

// console.log(stu.__proto__);

 

/*

* 1.原型链继承

*  特点: 1.简单粗暴的实现继承非常纯粹的继承关系

*       2.父类新增的原型方法或者属性子类都能够轻松的访问到

*       3.简单,容易实现

* 缺点:1.要想为子类新增方法/属性,需要在 new Person()之后进行添加,不能在构造函数中添加

*      2.无法实现多继承

*      3.来自原型对象的引用属性是所有实例共享的

*/

// function Student(){

//

// }

// //继承核心子类的原型指向父类的实例

// Student.prototype = new Person();

// var stu = new Student();

// stu.name = '车银优';

// console.log(stu.name);

// stu.eat();

 

/*

* 2.构造继承

*   优点:1.可以实现多继承 

*   2.解决1种子类实例共享父类属性的问题

*   缺点: 1.只能继承父类构造函数中的内容无法继承父类原型对象中的属性/方法

*         2.无法实现函数复用

*/

// function Student(name,gender,age,stu_id){

// //核心:使用父级构造函数来增强子类实例(没有用到原类型对象)

// Person.call(this,name,gender,age);

// this.stu_id = stu_id || 123456;

// }

// var stu = new Student("熊大","男",20,119);

// console.log(stu.name,stu.gender,stu.age,stu.stu_id);

 

/*

* 3.实例继承

* 特点: 1.不限制调用方式:可以 new Student() 也可以直接调用函数 student()

* 2.实例本质上是父类的实例不是子类的实例

*      3.不支持多继承的特点

*/

// function Student(name,gender,age,stu_id){

// //核心:为父类的实例添加新特性,然后作为子类的实例返回

// var per = new Person(name,gender,age);

// per.stu_id = stu_id;

// return per;

// }

// var stu = new Student("熊大","男",20,119);

// console.log(stu.name,stu.gender,stu.age,stu.stu_id);

// stu = Student("熊二","男",1,12345678);

// console.log(stu.name,stu.gender,stu.age,stu.stu_id);

 

/*

* 4.拷贝继承

*  特点:1.支持多继承

*       2.效率低,内容占用高

*       3.无法获取父类中不可枚举的方法

*/

// function Student(name,gender,age,stu_id){

// var  per = new Person(name,gender,age);

// //枚举遍历

// for(var k in per){

// Student.prototype[k] = per[k];

// }

// //特有属性

// Student.prototype.stu_id = stu_id

// }

// var stu = new Student("小猪佩奇","男",12,98);

// console.log(stu.name,stu.gender,stu.age,stu.stu_id);

 

/*

* 5.组合继承

*   核心: 将构造继承与原型继承复合在一起

*   特点: 1.既可继承福父类构造函数中的内容,也可以继承父类原型对象中的内容

*    2.子类可以灵活添加本类特有属性,不存在属性共享问题

*    3.两次调用父类的构造函数

*/

// function Student(name,gender,age,stu_id){

// Person.call(this,name,gender,age)

// this.stu_id = stu_id;

// }

// Student.prototype = new Person();

// Student.prototype.constructor = Student;

// var stu = new Student("小猪佩奇","男",12,98);

// console.log(stu.name,stu.gender,stu.age,stu.stu_id);

// stu.eat();

// stu.drink();

 

/*

  * 6.寄生组合继承

  * 特点: 1.融合了以上各种继承方式的优点

  *       2.实现复

  */

function Student(name,gender,age,stu_id){

Person.call(this,name,gender,age);

this.stu_id = stu_id;

}

//匿名函数的调用

(function(){

//创建了一个类

var Super = function(){};

//将该类的原型指向Person

Super.prototype = Person.prototype;

//子类Student的原型指向 Super的实例创建一个 Super类

Student.prototype = new Super();

Student.prototype.constructor = Student;

})();

var stu = new Student("小猪佩奇","男",12,98);

console.log(stu.name,stu.gender,stu.age,stu.stu_id);

stu.eat();

stu.drink();

 

</script>

</body>

</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jiojio冲冲冲

能帮助你是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值