zoukankan      html  css  js  c++  java
  • js 继承

    静态方法的继承

     function ClassA(name) {
                this.name = name;
                this.showSub = function (a, b) {
                    alert(a - b);
                }
                this.getName = function () {
                    alert(this.name);
                }
            }
            function ClassB() {
                this.showAdd = function (a, b) {
                    alert(a + b);
                }
            }
            function ClassC(name) {
                ClassA.call(this,name);
                ClassB.call(this);
            }
            var cc = new ClassC("aaaaaaaaaa");
            cc.showAdd(1, 8);
            cc.getName();
            cc.showSub(8, 1);

    原型方法继承

         function Person(name) {
                this.name = name;
            }
            Person.prototype.getName = function () {
                return this.name;
            }
            function Author(name, books) {
                Person.call(this, name);
                this.books = books;
            }
            Author.prototype = new Person();//将其Author的prototype属性指向Person对象实例
            Author.constructor = Author;//定义一个构造函数时,其默认的prototype对象是一个Object类型的实例,其constructor属性会
    //会被自动设置为该共造函数本身。如果手工将其prototype设置为另一个对象,那么新对象不会具有原有对象的constructor值,所以需要重新设定。 Author.prototype.getBooks
    =function(){ return this.books; } var author = []; author[0]=new Author("aa",'JavaScript Design Patterns'); author[1]= new Author("bb",'JavaScript Design Patterns'); alert(author[0].getName()); alert(author[1].getBooks());
  • 相关阅读:
    8皇后问题
    求1到n,n个整数的全排列
    求最小周期串
    如何用java完成一个中文词频统计程序
    蛇形矩阵
    第一个算法程序
    java 继承练习题8
    java 继承练习题7
    java 继承练习题6
    java 继承练习题5
  • 原文地址:https://www.cnblogs.com/cniteeq/p/3305447.html
Copyright © 2011-2022 走看看