zoukankan      html  css  js  c++  java
  • typeScript 之 (8) 继承,重写,super

    super是什么?

    在类方法中, super 就表示当前类的父类

    class Animal{
        name:string
        age:number
        constructor(name:string,age:number){
          this.name=  name
          this.age = age
        }
        sayHello(){
          console.log('动物在叫');
          
        }
      }
    
      class Dog extends Animal{
         sayHello(){
            super.sayHello(); 
        }
    
      }

    使用继承该如何写?

    在没有使用继承前,狗和喵喵在学说话如何写?

    (function(){
      //定义一个表示狗的类
      class Dog{
        name:string
        age:number
        constructor(name:string,age:number){
          this.name=  name
          this.age = age
        }
        sayHello(){
          console.log('汪汪汪');
          
        }
      }
    
      class Cat {
        name:string
        age:number
        constructor(name:string,age:number)
        {
          this.name = name
          this.age = age
        }
        sayHello(){
          console.log('喵喵喵');
          
        }
      }
    
      const dog = new Dog('旺财',18)
      const cat  = new  Cat('咪咪',10)
      dog.sayHello()
      cat.sayHello()
    
    }())
  • 相关阅读:
    ACM-超级楼梯
    clientt.c
    call.c
    answer.c
    aa.c
    client.c
    service.c
    自己动手开发jQuery插件
    apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出
    在Eclipse中制作SSH配置文件提示插件
  • 原文地址:https://www.cnblogs.com/zmztya/p/14748140.html
Copyright © 2011-2022 走看看