定义图形类(Shape),有length和 height属性,方法是area()用于求面积。定义两个子类分别是长方形(Rect)和三角形(Triangle),求它们的面积并输出。

  class shape{
                    constructor(length,height){
                        this.length=length
                        this.height=height
                    }
                    area(){

                    }
                }

        class  rect extends shape{
            constructor(length,height){
                super(length,height)
            }
            area(){
                return this.height*this.length
            }

        }
        class  Triangle extends shape{
               constructor(length,height){
                super(height,length)
               }
               area(){
                return  1/2*this.length*this.height
               }
        }

        let r=new rect(4,5)
        console.log('长方形的面积是',r.area());
        let b=new Triangle(5,6)
        console.log('三角形的面积是',b.area());




版权声明:本文为Weiyatong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/Weiyatong/article/details/127821428