TS - Public and private methods
by Rodwyn Moreno
TypeScript
class Avenger {
public name:string;
protected team:string;
private realName: string;
private canFight:boolean = false;
private sightsWon:number = 0;
constructor( name:string, team:string, realName:string ) {
this.name = name;
this.team = team;
this.realName = realName;
}
public bio():void{
let message: string = `${this.name} ${this.realName} ${this.team}`;
console.log(message);
}
public setTeam(newTeam: string): void {
if (this.validateTeam(newTeam)) {
this.team = newTeam;
}
}
private validateTeam(newTeam: string): boolean {
if (newTeam === this.team) {
return false;
} else {
return true;
}
}
}
let antman: Avenger = new Avenger("Antman", "cap", "Hank Pim");
antman.bio();
antman.setTeam("Ironman");
antman.bio();
//Note: copy and paste in http://www.typescriptlang.org/