微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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()

}())

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐