1class Info {
2 private name: string ;
3 constructor(n:string){
4 this.name = n ;
5 };
6 describe(){
7 console.log(`Your name is ${this.name}`);
8 }
9}
10
11const a = new Info('joyous');
12a.describe();
1class GenericNumber<T> {
2 zeroValue: T;
3 add: (x: T, y: T) => T;
4}
5
6let myGenericNumber = new GenericNumber<number>();
7myGenericNumber.zeroValue = 0;
8myGenericNumber.add = function(x, y) { return x + y; };
9
1class Greeter { greeting: string;
2 constructor(message: string) { this.greeting = message; }
3 greet() { return "Hello, " + this.greeting; }}
4let greeter = new Greeter("world");Try
1class Animal {
2 move(distanceInMeters: number = 0) {
3 console.log(`Animal moved ${distanceInMeters}m.`);
4 }
5}
6
7class Dog extends Animal {
8 bark() {
9 console.log("Woof! Woof!");
10 }
11}
12
13const dog = new Dog();
14dog.bark();
15dog.move(10);
16dog.bark();Try
1class Animal { public name: string;
2 public constructor(theName: string) { this.name = theName; }
3 public move(distanceInMeters: number) { console.log(`${this.name} moved ${distanceInMeters}m.`); }}Try