ES6面向对象

1、编写一个动物类(Animal),该类包含name的属性,和say的方法。 通过say方法可以打印动物说话了。编写一个Dog类继承动物类,要求该类中包含颜色的属性,该类重写say方法,要求打印父类的say方法里的内容,并且打印 动物颜色+动物名字+“叫了”。


<body>
    <script>
        class Animal{
            constructor(name){
                this.name = name
            }
            say(){
                
            }
        }
        class Dog extends Animal{
            constructor(name,color){
                super(name)
                super.say()
                this.color = color
            }
            say(){
                console.log(`${this.color}颜色的${this.name}叫了`)
            }
        }
        let animal = new Dog('老虎','黄色')
        animal.say()
    </script>
</body>

2.设计一个名为Location的类,定位二维数组中的最大值及其位置。这个类包括属性row(行下标)、column(列下标)和maxValue(最大值)。编写下面的函数,返回一个

Location的对象(对象中包含了二维数组的最大值、最大值的行下标和列下标):

        locateLargest(arr)
<script>
        class Location{
            constructor(){
            }
            

        }
        class LocationLargest extends Location{
            constructor(){            
                super()
            }
            array(arr){
               this.maxValue =0;
                //  this.maxValue = this.arr[0][0]
                for(let i=0;i<arr.length;i++){
                    for(let j = 0;j<arr[i].length;j++){
                        if(this.maxValue<arr[i][j]){
                            this.row = i 
                            this.column = j
                          this.maxValue = arr[this.row][this.column]
                        }
                    }
                } 
                console.log('该数组的最大值是'+this.maxValue,'最大值的行下标是',this.row+1,'最大值的列下标',this.column+1)
            }
            
        }
        let newArr = new Array([1,2,3,4],[5,6,7,9,8],[12,65,78,98])
        let arr_1 = new LocationLargest()
        
        console.log(arr_1.array(newArr))

        //方法二;
        class Location{
            constructor(row,column,maxValue){
                this._row = row;
                this._column = column
                this._maxValue = maxValue

            }
            get row (){
                return this._row
            }
            set row(val){
                this._row = val

            }
            get column (){
                return this._column
            }
            set column(val){
                this._column = val

            }
            get maxValue (){
                return this._maxValue
            }
            set maxValue(val){
                this._maxValue = maxVal

            }
        }
        function LocationLargest(arr){
            let max_lacate = new Location(0,0,arr[0][0])
            for(let i = 0;i<arr.length;i++){
                for(let j=0;j<arr[i].length;j++){
                    if(max_lacate.maxVal<arr[i][j]){
                        max_lacate.maxVal = arr[i][j]
                        max_lacate.row = 1
                        max_lacate.column = j
                    }
                }
            }
            // 3.返回Location类的对象
        return max_lacate
        }
        let arr = [[35,45,23,66],[78,89,95,102],[55,66,102,98]]
        let located = LocationLargest(arr)
    </script>

 3、设计一个公共汽车类Bus,有速度(speed)和人数(count)两个属性,有加速(speedUp)、减速(slowDown)、上人(goUp)、下人(goDown),停车(stop)、启动(start)等方法。创建一个对象,车上有35人,开始发车,中间下了13人,又上了25人,请问到站还有多少人 用程序实现。

<script>
        class Bus{
            constructor(){
                
            }
            speedUp(){
                console.log('公交车正在加速')
            }
            speedDown(){
                console.log('公交车正在减速')
            }
            stop(){
                console.log('公交车停车')
            }
            start(){
                console.log('公交车启动')
            }
        }
        class currentPeople extends Bus{
            constructor(speed,count,num,num1){
                super()
                this.speed = speed;
                this.count = count;
                this.num = num
                this.num1 = num1
            }
            goUp(){
                return this.count+this.num
            }
            goDown(){
                return this.num1
            }
            show(){
                console.log('公交车目前有'+(this.goUp()-this.goDown()),'公交车车速为'+this.speed)
            }
        }
        let c1 = new currentPeople(100,35,15,13)
        c1.show()
        c1.goDown()
        c1.goUp()
    </script>

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

<script>
        class Shape{
            constructor(length,height){
                this.length = length;
                this.height = height;

            }
        }
        class Rect extends Shape{
            constructor(length,height){
                super(length,height)
            }
            area(){
              return  this.length*this.height
            }
            show(){
                console.log('长方形的面积',this.area())
            }
        }
        class Triangle extends Shape{
            constructor(length,height){
                super(length,height)
            }
            area(){
              return  (this.length*this.height)*1/2
            }
            show(){
                console.log('三角形的面积',this.area())
            }
        }
        // 方法一
        // let c1 = new Rect(10,20)
        // c1.show()
        // c1.area()
        // let c2 = new Triangle(10,20)
        // c2.show()
        // c2.area()

        // 方法二
        function mianJi (shape){
            shape.show()
        } 

        let s1 = new Shape() //s1是基类的对象

        s1 = new Rect(10,20)
        mianJi(s1)

        s1 =new Triangle(10,20)
        mianJi(s1)
    </script>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值