zoukankan      html  css  js  c++  java
  • [TypeScript] Sharing Class Behavior with Inheritance in TypeScript

    Typescript classes make inheritance much easier to write and understand. In this lesson we look into how to set up inheritance with Typescript classes, extends and super.

    class ComicBookCharacter (
      constructor{
        public alias: string, public health: number , public strength: number,
        protected secretIdentity: string
      ) {}
    }
    
    class SuperVillain extends ComicBookCharacter {
      flaws = ["hubris", "always explains evil plan"];
    
      constructor(a, b, c, d) {
        console.log('${this.alias} eats kittens!!!');
        super(a, b, c, d);
      }  
    }

    To review, we can set up inheritance with the extends keyword. The protected access modifier can't be accessed outside of a class just like the private access modifier, but it can be accessed in derived classes.

    If you don't define a constructor, the derived class will use the base class's constructor. If you do define the constructor in a derived class, super must be called before anything else can happen.

  • 相关阅读:
    ActiveMQ持久化机制
    ActiveMQ的使用
    ActiveMQ解释
    Linux CentOS安装Tomcat
    nginx使用Keepalived
    Session共享解决方案
    Spring框架AOP使用扩展
    Myabtis测试(二)错题整理分析
    初识Spring及打印机案例
    MyBatis注解
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5958923.html
Copyright © 2011-2022 走看看