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.

[TypeScript] Creating a Class in TypeScript的更多相关文章

  1. [Typescript] Introduction to Generics in Typescript

    If Typescript is the first language in which you've encountered generics, the concept can be quite d ...

  2. 学习TypeScript,笔记一:TypeScript的简介与数据类型

    该文章用于督促自己学习TypeScript,作为学笔记进行保存,如果有错误的地方欢迎指正 2019-03-27  16:50:03 一.什么是TypeScript? TypeScript是javasc ...

  3. [TypeScript] Custom data structures in TypeScript with iterators

    We usually think of types as something that can define a single layer of an object: with an interfac ...

  4. [Typescript] Specify Exact Values with TypeScript’s Literal Types

    A literal type is a type that represents exactly one value, e.g. one specific string or number. You ...

  5. [TypeScript] Overload a Function with TypeScript’s Overload Signatures

    Some functions may have different return types depending on the types of the arguments with which th ...

  6. [TypeScript] Represent Non-Primitive Types with TypeScript’s object Type

    ypeScript 2.2 introduced the object, a type that represents any non-primitive type. It can be used t ...

  7. [TypeScript] Understand lookup types in TypeScript

    Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the proper ...

  8. 深入浅出TypeScript(2)- 用TypeScript创建web项目

    前言 在第一篇中,我们简单介绍了TypeScript的一些简单语法,那么如果我们只是简单使用TypeScript开发一个web项目,应该做哪些准备?接下来我们就结合TypeScript和Webpack ...

  9. Typescript node starter 1.Express Typescript

    启动项目 Express 是一个nodejs框架,用于构建Web后端应用程序.它非常的灵活,你可以用你喜欢的方式去使用他.在这个系列文章里,记录了我使用typescript express去构建一个w ...

随机推荐

  1. ie中 专有的注释

    http://www.cnblogs.com/liluping860122/p/3539165.html

  2. logout命令用于退出当前登录的Shell

    logout命令用于退出当前登录的Shell

  3. SQL、Linq相关字段搜索

    结合一些分词组件,如盘古,对于用户查询关键字红按钮很容易分出 ‘红’ ‘按钮’二个单词 我们假设产品名称列里面是红色,规格里面是按钮 /* 普通sql实现全文搜索declare @key1 nvarc ...

  4. POJ——T 3461 Oulipo

    http://poj.org/problem?id=3461 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42698   ...

  5. Hadoop权威指南学习笔记三

    HDFS简单介绍 声明:本文是本人基于Hadoop权威指南学习的一些个人理解和笔记,仅供学习參考.有什么不到之处还望指出,一起学习一起进步. 转载请注明:http://blog.csdn.net/my ...

  6. matlab (.m)文件生成 windows 可执行(.exe)文件

    mex -setup:设置 C 语言编译器:(如果本地安装有 visual studio 20xx 集成开发环境,则会自动选择其下的 C/C++ 编译器 ) 将运行时环境(runtime enviro ...

  7. 有点坑爹的GDALComputeRasterMinMax函数

    作者:朱金灿 来源:http://blog.csdn.net/clever101 GDALComputeRasterMinMax函数是gdal库为了求取指定波段的极值而提供的接口.最近看了这个接口的源 ...

  8. 深入了解Linux远程桌面

    本文转载于:http://www.linux521.com/2009/system/201004/11001.html 已发表在<网管员世界>2010年3月杂志             本 ...

  9. goinstall

    [背景] 折腾: [记录]go语言中通过log4go实现同时输出log信息到log文件和console 期间,以: http://code.google.com/p/log4go/ 为例,如何安装第三 ...

  10. vue移动端上拉加载更多

    LoadMore.vue <template> <div class="load-more-wrapper" @touchstart="touchSta ...