首页 > 其他分享 >TypeScript之super

TypeScript之super

时间:2022-11-27 11:22:33浏览次数:38  
标签:TypeScript name age sayHello super string 构造函数

 示例

ts文件:

(function () {
    class Animal {
        name: string;

        constructor(name: string) {
            this.name = name;
        }

        sayHello() {
            console.log('动物在叫~');
        }
    }

    class Dog extends Animal{

        age: number;

        constructor(name: string, age: number) {
            // 如果在子类中写了构造函数,在子类构造函数中必须对父类的构造函数进行调用
            super(name); // 调用父类的构造函数
            this.age = age;
        }

        sayHello() {
            // 在类的方法中 super就表示当前类的父类
             super.sayHello();

            console.log('汪汪汪汪!');
        }

    }

    const dog = new Dog('旺财', 3);
    dog.sayHello();
})();

 

 

 

 

标签:TypeScript,name,age,sayHello,super,string,构造函数
From: https://www.cnblogs.com/anjingdian/p/16929314.html

相关文章

  • TypeScript继承
    继承  * -使用继承后,子类将会拥有父类所有的方法和属性  * -通过继承可以将多个类中共有的代码写在一个父类中,  *   这样只需要写一次即可......
  • TypeScript 高级类型
    TypeScript高级类型class类类型兼容性交叉类型泛型和keyof索引签名类型和索引查询类型映射类型class类classPerson{  age:number//......
  • TypeScript之构造函数和this
     示例:classDog{name:string;age:number;//constructor被称为构造函数//构造函数会在对象创建时调用constructor(name:string,age:......
  • TypeScript之类
    TypeScript中的类的定义与使用示例//使用class关键字来定义一个类/**对象中主要包含了两个部分:*属性*方法**/classPerson{/**......
  • TypeScript/Javascript 泛型字典
    typescript是javaScript的超集,相当于把弱类型的js变成了强类型的语言,并且实现了封装(成员私有),更方便面向对象编程。然鹅,typescript并没有扩增原生JS的内容,比如:支持了import......
  • 【浅谈Java】this和super的用法与区别
    在Java的学习与开发者我们经常遇到this和super关键字,那么它们的用法和区别是什么呢?一、this关键字1.this是什么?this是自身的一个对象,代表对象本身,可以理解为:指向对象本......
  • TypeScript学习笔记-05webpack打包
    1.使用命令npminit-y生成项目package.json,这个文件是项目的基本信息,方便我们对项目进行管理,如图所示。2.使用命令 npmi-Dwebpackwebpack-clitypescriptts-load......
  • [Typescript] 118. Hard - IsRequiredKey
    Implementageneric IsRequiredKey<T,K> thatreturnwhether K arerequiredkeysof T .ForexampletypeA=IsRequiredKey<{a:number,b?:string},'a'>......
  • [Typescript] 117. Hard - ClassPublicKeys
    Implementthegeneric ClassPublicKeys<T> whichreturnsallpublickeysofaclass.Forexample:classA{publicstr:stringprotectednum:numberpri......
  • TypeScript学习笔记-04 tsconfig.json配置文件
    tsconfig.json一般常用的配置如下所示,可以按需要进行配置。{/*tsconfig.json是ts编译器的配置文件,ts编译器可以根据他的信息来对代码进行编译//in......