Typescript classes make traditional object oriented programming easier to read and write. In this lesson we learn about class syntax, what the constructor is and some interesting variable features.

interface Opponent {
health: number;
alias: string;
} class ComicBookCharacter { private team: {
name: string;
members: ComicBookCharacter[]
}; static createAndAssignTeam(teamName: string, members: ComicBookCharacter[]) {
let team = {
name: teamName,
members: members
}; members.forEach((member)=>{
member.team = team;
})
} constructor(public alias: string,
public health: number,
public strength: number,
private secretIdentity: string) { } getTeamName(){
console.log(`${this.alias} is from ${this.team.name}`);
return this.team.name;
} attackFunc(opponent: Opponent, attackWith) {
opponent.health -= attackWith;
console.log(`${this.alias} attacked ${opponent.alias}, who's health = ${opponent.health}`);
return opponent.health;
} getSecretIdentity() {
if (this.secretIdentity) {
console.log(`${this.alias} is ${this.secretIdentity}`);
} else {
console.log(`${this.alias} has no secret identity`);
}
}
} const Sparky = new ComicBookCharacter("Sparky", , , "Thunder");
const Rainer = new ComicBookCharacter("Rainer", , , "Water"); ComicBookCharacter.createAndAssignTeam('Eevee', [Sparky,Rainer]);
Sparky.getTeamName();

To review, we've learned about access modifiers and the difference between public and private. The constructor is run when the classinstance is initialized, and the shorthand for setting class properties allows us to write less code. We've learned that static properties can only be referenced from the class, not the instance, and how staticproperties have access to an instances private properties.

最新文章

  1. MySQL数据表生成ER图
  2. 【uva】1220 Party at Hali-Bula
  3. oc-23-static
  4. PHP: 深入pack/unpack 字节序
  5. ArcSDE 10.1安装、配置、连接 (SQL Server 2008)
  6. String.PadLeft - 格式对齐
  7. mysql学习(五)-字段属性
  8. HDU 1498 50 years, 50 colors(最小点覆盖,坑称号)
  9. [贪心][高精]P1080 国王游戏(整合)
  10. js文件加载优化
  11. Java开发笔记(四十)日期与字符串的互相转换
  12. JS 实现兼容IE图片向左或向右翻转
  13. apache启动失败
  14. Perl的输出:print、say和printf、sprintf
  15. JS获取验证码后倒计时不受刷新及关闭影响
  16. Vue基础进阶 之 自定义指令
  17. 如何实现浏览器向服务器伪造refer?
  18. url中含有%
  19. Zookeeper命令行zkCli.sh&zkServer.sh的使用(四)
  20. Delphi 实现不规则窗体

热门文章

  1. Redis安装以及配置
  2. HDU 3594 The trouble of Xiaoqian 混合背包问题
  3. Centos安装FastDFS+Nginx(一天时间搞定)
  4. 03003_MySQL
  5. 图片工具GraphicsMagick的安装配置与基本使用
  6. 用了Redis里面的map和set
  7. 高速数论变换(NTT)
  8. 雅黑PHP探针 For PHP7
  9. actionMode - 在屏幕中的显示位置设置
  10. zendiscovery 的Ping机制