TypeScript - Interface
TypeScript Interface:
* Class can implement multiple interfaces.
* Define a interface with method contract.
Eg:
export interface Coach {
getDailyWorkout(): string;
}
import { Coach } from "./Coach";
export class GolfCoach implements Coach {
getDailyWorkout(): string {
return "Hit 100 balls at the golf range.";
}
}
import { Coach } from "./Coach";
export class CricketCoach implements Coach {
getDailyWorkout(): string {
return "Practice your spin bowling technique.";
}
}
import { CricketCoach } from "./CricketCoach";
import { GolfCoach } from "./GolfCoach";
import { Coach } from "./Coach";
let myCricketCoach = new CricketCoach();
let myGolfCoach = new GolfCoach();
// declare an array for coaches ... initially empty
let theCoaches: Coach[] = [];
// add the coaches to the array
theCoaches.push(myCricketCoach);
theCoaches.push(myGolfCoach);
for (let tempCoach of theCoaches) {
console.log(tempCoach.getDailyWorkout());
}
* Class can implement multiple interfaces.
* Define a interface with method contract.
Eg:
export interface Coach {
getDailyWorkout(): string;
}
import { Coach } from "./Coach";
export class GolfCoach implements Coach {
getDailyWorkout(): string {
return "Hit 100 balls at the golf range.";
}
}
import { Coach } from "./Coach";
export class CricketCoach implements Coach {
getDailyWorkout(): string {
return "Practice your spin bowling technique.";
}
}
import { CricketCoach } from "./CricketCoach";
import { GolfCoach } from "./GolfCoach";
import { Coach } from "./Coach";
let myCricketCoach = new CricketCoach();
let myGolfCoach = new GolfCoach();
// declare an array for coaches ... initially empty
let theCoaches: Coach[] = [];
// add the coaches to the array
theCoaches.push(myCricketCoach);
theCoaches.push(myGolfCoach);
for (let tempCoach of theCoaches) {
console.log(tempCoach.getDailyWorkout());
}
Comments
Post a Comment