zoukankan      html  css  js  c++  java
  • javascript实现继承

    实现继承示例一:
        <script language="javascript" type="text/javascript">
            function ClassA(sColor)
            {
                this.color=sColor;
            }
            ClassA.prototype.sayColor=function()
            {
                alert(this.color);
            }
            function ClassB(sColor,sName)
            {
                ClassA.call(this,sColor);
                this.name=sName;
            }
            ClassB.prototype=new ClassA();
            ClassB.prototype.sayName=function()
            {
                alert(this.name);
            }
            var objA=new ClassA("red");
            var objB=new ClassB("blue","Nicholas");
            objA.sayColor();
            objB.sayColor();
            objB.sayName();
        </script>
      
    实现继承示例二:
        <script language="javascript" type="text/javascript">
            function Polygon(iSides)
            {
                this.sides=iSides;
            }
            Polygon.prototype.getArea=function()
            {
                return 0;
            }
            function Trangle(iBase,iHeight)
            {
                Polygon.call(this,3);
                this.base=iBase;
                this.height=iHeight;
            }
            Trangle.prototype=new Polygon();
            Trangle.prototype.getArea=function()
            {
                return 0.5*this.base*this.height;
            };
            function Rectangle(iLength,iWidth)
            {
                Polygon.call(this,4);
                this.length=iLength;
                this.width=iWidth;
            }
            Rectangle.prototype=new Polygon();
            Rectangle.prototype.getArea=function()
            {
                return this.length*this.width;
            };
            var triangle=new Trangle(12,4);
            var rectangle=new Rectangle(2,2);
            document.write("三角形的边数:",triangle.sides,"<br />");
            document.write("三角形的面积:", triangle.getArea(), "<br />");
            document.write("矩形的边数:", rectangle.sides, "<br />");
            document.write("矩形的面积:", rectangle.getArea(), "<br />");
        </script>
  • 相关阅读:
    Linux终端基本命令
    Markdown基本语法
    谷歌浏览器解决”此Flash Player与您的地区不相容“
    谷歌浏览器不可以默认允许flash的解决方法
    MySQL8.0登陆方式
    谷歌浏览器安装位置自定义
    java生成六位验证码
    对AJAX的理解
    对servlet请求的理解
    js60秒倒计时
  • 原文地址:https://www.cnblogs.com/chengpeng/p/2134970.html
Copyright © 2011-2022 走看看