Answers for "TypeScript Class Inheritance"

1

TypeScript Class Inheritance Example

class Animal {  private name: string;  constructor(theName: string) {    this.name = theName;  }}
class Rhino extends Animal {  constructor() {    super("Rhino");  }}
class Employee {  private name: string;  constructor(theName: string) {    this.name = theName;  }}
let animal = new Animal("Goat");let rhino = new Rhino();let employee = new Employee("Bob");
animal = rhino;animal = employee;Type 'Employee' is not assignable to type 'Animal'.
  Types have separate declarations of a private property 'name'.2322Type 'Employee' is not assignable to type 'Animal'.
  Types have separate declarations of a private property 'name'.Try
Posted by: Guest on July-14-2021
5

classes in typescript

class Info {
  private name: string ;
  constructor(n:string){
    this.name = n ;
  };
  describe(){
    console.log(`Your name is  ${this.name}`);
  }
}

const a = new Info('joyous');
a.describe();
Posted by: Guest on October-17-2020
0

TypeScript Class Inheritance

class Animal {
  move(distanceInMeters: number = 0) {
    console.log(`Animal moved ${distanceInMeters}m.`);
  }
}

class Dog extends Animal {
  bark() {
    console.log("Woof! Woof!");
  }
}

const dog = new Dog();
dog.bark();
dog.move(10);
dog.bark();
Posted by: Guest on July-14-2021
0

class inheritance in typescript

// Parent class
class Info {
  protected name: string ;
  constructor(n:string){
    this.name = n ;
  };
  describe(){
    console.log(`Your name is  ${this.name}`);
  }
}

//inherited class (you can overwrite methods of parent class, super is used to
// connect to the parent parameter(s) . )

class Detail extends Info{
  constructor(name:string, public age:number){
    super(name);
  }
  findAge(){
      console.log(`${this.name} age is ${this.age}`)
  }
}

const b = new Detail('jank', 23);
b.describe();
b.findAge();
Posted by: Guest on October-17-2020
0

TypeScript Class Inheritance

class Animal {  name: string;  constructor(theName: string) {    this.name = theName;  }  move(distanceInMeters: number = 0) {    console.log(`${this.name} moved ${distanceInMeters}m.`);  }}
class Snake extends Animal {  constructor(name: string) {    super(name);  }  move(distanceInMeters = 5) {    console.log("Slithering...");    super.move(distanceInMeters);  }}
class Horse extends Animal {  constructor(name: string) {    super(name);  }  move(distanceInMeters = 45) {    console.log("Galloping...");    super.move(distanceInMeters);  }}
let sam = new Snake("Sammy the Python");let tom: Animal = new Horse("Tommy the Palomino");
sam.move();tom.move(34);Try
Posted by: Guest on July-14-2021

Code answers related to "TypeScript Class Inheritance"

Browse Popular Code Answers by Language