继承与多态笔记

继承与多态

继承和多态同一件事情的两种完全不同的侧重:
它们只会在子一级生效,并不会影响父一级构造函数的方法。
继承:侧重是从父一级构造函数,继承到的属性和方法。
多态:侧重于子一级,自己重写和新增的属性和方法。

继承

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
        <title>Document</title>
        <script>  function Dog({name, type, age}){
                //this = new Object();
                //添加属性
                this.name = name;
                this.type = type;
                this.age = age;
            }
          
            /* 
                通过构造函数的原型添加方法
             */
             Dog.prototype = {
                 run: function(){
                     alert(this.name + "会飞快的奔跑");
                 },
                 showSelf: function(){
                     alert(`这是一个${this.type}的,${this.age}岁的,叫${this.name}的小狗`);
                 }
             }
             /* 
                分类更加细分的构造函数。继承
              */
            function Teddy({name, type, age, color}){
                //this = new Object();
                //1、继承父一级构造函数所有的属性
                //构造函数的伪装
                Dog.call(this, {
                    name: name,
                    type: type,
                    age: age
                })
                //添加自己的属性
                this.color = color;

                //return this;
            }

            /* 
                原型链继承
            */
            // Teddy.prototype = Dog.prototype;  非常错误的写法
            for(var funcName in Dog.prototype){
                Teddy.prototype[funcName] = Dog.prototype[funcName];
            }


            Teddy.prototype.showColor = function(){
                alert(this.color);
            }


            var xiaohong = new Teddy({
                name: "小红",
                type: "泰迪",
                age: 10,
                color: "红色"
            })

            xiaohong.showSelf();
            xiaohong.showColor();

            var xiaohei = new Dog({
                name: "小黑",
                type: "拉布拉多",
                age: 5
            });
            alert(xiaohei.showColor);
        </script>
    </head>
    <body>
        
    </body>
</html>

多态

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
        <title>Document</title>
        <script>
            function Dog({name, type, age}){
                //this = new Object();
                //添加属性
                this.name = name;
                this.type = type;
                this.age = age;
            }
            /* 
                通过构造函数的原型添加方法
             */
             Dog.prototype = {
                 run: function(){
                     alert(this.name + "会飞快的奔跑");
                 },
                 showSelf: function(){
                     alert(`这是一个${this.type}的,${this.age}岁的,叫${this.name}的小狗`);
                 }
             }
             /* 
                分类更加细分的构造函数。继承
              */
            function Teddy({name, type, age, color}){
                //this = new Object();
                //1、继承父一级构造函数所有的属性
                //构造函数的伪装
                Dog.call(this, {
                    name: name,
                    type: type,
                    age: age
                })
                //添加自己的属性
                this.color = color;

                //return this;
            }

            /* 
                原型链继承
             */
            // Teddy.prototype = Dog.prototype;  非常错误的写法
            for(var funcName in Dog.prototype){
                Teddy.prototype[funcName] = Dog.prototype[funcName];
            }

            //在子一级构造函数重写showSelf方法
            /* 
                只会在子一级生效,并不会影响父一级构造函数的方法。

                继承和多态同一件事情的两种完全不同的侧重:
                继承:侧重是从父一级构造函数,继承到的属性和方法。
                多态:侧重是,子一级,自己重写和新增的属性和方法。
             */
            Teddy.prototype.showSelf = function(){
                alert(`这是一个${this.type}的,${this.age}岁的,是${this.color}的,叫${this.name}的小狗`);
            }


            Teddy.prototype.showColor = function(){
                alert(this.color);
            }


            var xiaohong = new Teddy({
                name: "小红",
                type: "泰迪",
                age: 10,
                color: "红色"
            })

            xiaohong.showSelf();
          

            var xiaohei = new Dog({
                name: "小黑",
                type: "拉布拉多",
                age: 5
            });
            xiaohei.showSelf();
        </script>
    </head>
    <body>
        
    </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值