zoukankan      html  css  js  c++  java
  • js面向对象举例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    </head>
    <body>
    <script type="text/javascript">

    //-------------抽象类形状--------------
    function Shape(edges) {
    this.edges = edges;
    }

    Shape.prototype.getArea
    = function () {
    return -1;
    }

    Shape.prototype.getEdges
    = function () {
    return this.edges;
    }

    //--------------三角形----------------
    function Triangle(bottom, height) {
    Shape.call(
    this, 3);
    this.bottom = bottom;
    this.height = height;
    }

    //继承
    Triangle.prototype = new Shape();
    //重写方法
    Triangle.prototype.getArea = function () {
    return 0.5 * this.bottom * this.height;
    }

    //---------------矩形----------------
    function Rectangle(bottom, height) {
    Shape.call(
    this, 4);
    this.bottom = bottom;
    this.height = height;
    }
    //继承
    Rectangle.prototype = new Shape();
    //重写方法
    Rectangle.prototype.getArea = function () {
    return this.bottom * this.height;
    }


    //-------------测试-------------------
    var tri = new Triangle(4, 5);

    document.write(tri.getEdges()
    + "<br>");
    document.write(tri.getArea()
    + "<br>");

    var rect = new Rectangle(20, 40);
    document.write(rect.getEdges()
    + "<br>");
    document.write(rect.getArea()
    + "<br>");
    </script>
    </body>
    </html>
  • 相关阅读:
    常见银行编码收集
    kafka集群在消息消费出现无法找到topic分区的处理解决
    find命令通过排序只保留最新的文件目录
    Git fetch和git pull的区别
    git 常用命令
    wordpress模板修改及函数说明
    webbench进行压力测试
    git存储用户名与密码
    导出putty配置
    一个成功的Git分支模型
  • 原文地址:https://www.cnblogs.com/zfc2201/p/2403904.html
Copyright © 2011-2022 走看看