Buscar..


Observaciones

Este es un ejemplo básico que extiende una clase de automóvil genérico y define un método de descripción de automóvil.

Encuentra más ejemplos de TypeScript aquí - Ejemplos de TypeScript GitRepo

1 ejemplo básico de herencia de clase usando extended y super keyword

Una clase de automóvil genérico tiene alguna propiedad de automóvil y un método de descripción.

class Car{
    name:string;
    engineCapacity:string;

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

    describeCar(){
        console.log(`${this.name} car comes with ${this.engineCapacity} displacement`);
    }
}

new Car("maruti ciaz","1500cc").describeCar();

HondaCar amplía la clase de autos genéricos existentes y agrega una nueva propiedad.

class HondaCar extends Car{
    seatingCapacity:number;

    constructor(name:string,engineCapacity:string,seatingCapacity:number){
        super(name,engineCapacity);
        this.seatingCapacity=seatingCapacity;
    }

    describeHondaCar(){
        super.describeCar();
        console.log(`this cars comes with seating capacity of ${this.seatingCapacity}`);
    }
}
new HondaCar("honda jazz","1200cc",4).describeHondaCar();

2 ejemplo de variable de clase estática: cuente la cantidad de tiempo que se invoca el método

aquí countInstance es una variable de clase estática

class StaticTest{
    static countInstance : number= 0;
    constructor(){
        StaticTest.countInstance++;
    }
}

new StaticTest();
new StaticTest();
console.log(StaticTest.countInstance);


Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow