Skip to content

Instantly share code, notes, and snippets.

@Caballerog
Last active May 31, 2022 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Caballerog/4d522ae2fbd11339ff547586099b9cb9 to your computer and use it in GitHub Desktop.
Save Caballerog/4d522ae2fbd11339ff547586099b9cb9 to your computer and use it in GitHub Desktop.
abstract class Character {
protected _power: number;
private _name: string;
constructor(name:string, power: number = 0 ){
this._name = name;
this._power = power;
}
calculateFightPower(): number {
return this._power;
}
get name(): string {
return this._name;
}
}
class Heroe extends Character{
calculateFightPower() {
return super.calculateFightPower() * 2;
}
}
class Villain extends Character {
calculateFightPower() {
return super.calculateFightPower() + 100;
}
}
class Human extends Character{
calculateFightPower() {
return super.calculateFightPower() + 10;
}
}
class CharacterService {
characters: Character[] = [
new Heroe("Mario", 100),
new Villain("Koopa", 85),
new Human("Carlos", 30),
];
constructor() {
const power = this.calculatePowerTeam(this.characters);
console.log(`The power of the team is ${power}`);
}
calculatePowerTeam(characters: Character[]){
let power = 0;
for (const character of characters) {
power += character.calculateFightPower();
}
return power;
}
}
new CharacterService();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment