js-面向对象=继承-多态

//        1.通过原型为一个已经编写好的类型增加一个扩展方法
        //比如给string增加一个sayhello 或者addwjx方法
        var s = '今天天气好晴朗,处处好风光';
        String.prototype.sayHello = function () {
            alert('我是字符串');
        };
        s.sayHello();
        String.prototype.addStar = function () {
            alert('☆' + this + '☆');
        };
        s.addStar();


        //2.通过原型继承对象

        function Person(name, age, gender) {
            this.name = name;
            this.age = age;
            this.gender = gender;
        }
        Person.prototype.sayHello = function () {
            alert('我是Person对象的SayHello,' + this.name + this.age + this.gender);
        };


            //2.1继承

        function Student(id,classname) {
            this.stId = id;
            this.classname = classname;
        }
        var p = new Person('', 0, '');
        Student.prototype = p;
        var s = new Student(002, '远洋班')
        s.name = '王宝宝';
        s.age = 19;
        s.gender = '女';
        s.sayHello();
        //2.2重写
        s.sayHello = function () {
            alert('这是student类的sayhello' + this.name + this.age + this.gender)
        };
        s.sayHello();
            //2.3继承关系:对象继承父类,父类继承父类的父类,最终继承与object
发布了55 篇原创文章 · 获赞 4 · 访问量 1400

猜你喜欢

转载自blog.csdn.net/BowenXu11/article/details/105244640