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. 49.Node.js RESTful API

    转自:http://www.runoob.com/nodejs/nodejs-express-framework.html 什么是 REST? REST即表述性状态传递(英文:Representati ...

  2. channels

    package main import ( "fmt" "time" "strconv") func pinger(c chan strin ...

  3. 实现CSS样式垂直水平完全居中

    1.水平居中 a.内联元素(inline or inline-*)居中? 你可以让他相对父级块级元素居中对齐 .center-children { text-align: center; } b.块级 ...

  4. Python的正则表达概述

    本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例.本文的内容不包括如何编写高效的正则表达式.如何优化正则表达式,这些主题请查看其他教程 ...

  5. fsck---于检查并且试图修复文件系统中的错误

    fsck命令被用于检查并且试图修复文件系统中的错误.当文件系统发生错误四化,可用fsck指令尝试加以修复. -a:自动修复文件系统,不询问任何问题: -A:依照/etc/fstab配置文件的内容,检查 ...

  6. JS原生方法被覆盖后的恢复办法

    alert 被覆盖 今天装修博客园,调试了下JS代码发现 alert() 方法被官方覆盖了,查看源码得知 alert 的功能被替换成了 console.log. 恢复 var _frame = doc ...

  7. PHP截取字符串长度

    <?php function str_cut($string, $start=0,$length, $dot = '..') {    $strlen = strlen($string);    ...

  8. 【Hello 2018 D】Too Easy Problems

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 可以考虑把所有的题目按照ai排序. 然后顺序考虑最后做出来的题目个数和第i道题目的ai一样. 则1..i-1这些题目就没有用了. 值 ...

  9. Windows学习总结(3)——成为电脑高手必备的cmd命令大全

    曾经看电影和电视里面电脑黑客快速敲击电脑键盘,一行行命令在电脑屏幕闪过,一个回车过后,一排排英文象走马灯一样在屏幕上转瞬即逝,那才是我们梦寐以求的高手,有木有!实际上,不光是黑客和系统维护人员,一般的 ...

  10. Git学习总结(2)——初识 GitHub

    1. 写在前面 我一直认为 GitHub 是程序员必备技能,程序员应该没有不知道 GitHub 的才对,没想到这两天留言里给我留言最多的就是想让我写关于 GitHub 的教程,说看了不少资料还是一头雾 ...