定义类

class Person {
name: string; //属性
constructor(_name: string) {
this.name = _name;
} //构造函数
sayHello(): string {
return "Hi,everyone"
} //方法
} let firstOne = new Person("Fred") //实例化类

继承:继承使用关键字extends,调用父类使用super,子类继承父类的属性和方法,并且子类可以改写父类的属性和方法

class Animal {
name: string;
constructor(_name: string) {
this.name = _name;
}
skinColour(color: string = "black"): void {
console.log(`${this.name} skin colour is ${color}`)
}
} class Horse extends Animal {
constructor(name: string) { super(name) }
skinColour(color: string = "brown"): void {
console.log(`I'am ${this.name}`);
super.skinColour("brown");
}
} let horse = new Horse("horse");
horse.skinColour()
// I'am horse
// horse skin colour is brown

public、private、protected、readonly

  • public(不声明默认都为public,也可以显示的设置为public)
class Person {
public name: string; //属性
public constructor(_name: string) {
this.name = _name;
} //构造函数
public sayHello(): string {
return "Hi,everyone"
} //方法
} let firstOne = new Person("Fred") //实例化类
  • private(private的成员不能被外部访问;比较带有privateprotected成员的类型时,两个类型兼容的条件是private或protected的成员必须相同切来至同一个声明(同一个类))
class Person {
private name: string;
public constructor(_name: string) {
this.name = _name;
}
} class Employee {
private name: string;
public constructor(_name: string) {
this.name = _name;
}
} let firstOne = new Person("Fred")
console.log(firstOne.name) //error: Property 'name' is private;
let lastOne = new Employee("Fred")
firstOne = lastOne // error: Type 'Employee' is not assignable to type 'Person'.Types have separate declarations of a private property 'name'.
  • protected(protected和private相似,但protected成员可以在派生类中访问(能被继承,但不能在实例中访问,若构造函数是protected,则不能被实例化,只能被继承))
class Person {
protected name: string;
protected constructor(_name: string) {
this.name = _name;
}
} class Employee extends Person {
private department: string;
public constructor(name: string,department:string) {
super(name);
this.department = department;
}
} let Bob = new Person; //error: Constructor of class 'Person' is protected
let fred = new Employee("fred","test");
console.log(fred.name) //error: Property 'name' is protected
  • readonly(设置属性为只读,必须在声明时或构造函数里初始化)
class Person {
readonly name: string;
constructor(_name: string) {
this.name = _name;
}
} let fred = new Person("fred");
fred.name = "Bob" //error: Cannot assign to 'name' because it is a constant or a read-only property.

参数属性(参数属性通过给构造函数参数添加一个访问限定符来声明(public,private,protected),把声明和赋值合并至一处)

class Person {
constructor(private name: string) { }
sayHello(): void {
console.log(`my name is ${this.name}`)
}
} let fred = new Person("fred");
fred.sayHello() //my name is fred

存取器(get、set   只带有 get不带有set的存取器自动被推断为readonly

let passcode = "secret passcode";

class Employee {
private _fullName: string; get fullName(): string {
return this._fullName;
} set fullName(newName: string) {
if (passcode && passcode == "secret passcode") {
this._fullName = newName;
}
else {
console.log("Error: Unauthorized update of employee!");
}
}
} let employee = new Employee();
employee.fullName = "Bob Smith";
if (employee.fullName) {
console.log(employee.fullName);
}

静态属性(static,不能被实例访问,在类里面访问时,需要加上类名)

class Person {
static height:number = 180;
constructor(private name: string) { }
sayHello(): void {
console.log(`my name is ${this.name}, I height is ${Person.height}`)
}
} let fred = new Person("fred");
fred.sayHello() //my name is fred, I height is 180

抽象类(abstract,抽象类做为其它派生类的基类使用。 它们一般不会直接被实例化。抽象类中的抽象方法不包含具体实现并且必须在派生类中实现)

abstract class Person {
constructor(public name: string) { }
abstract sayHello():void;
} class Empoloy extends Person{
constructor(){
super("Fred")
}
sayHello(){
console.log(`my name is ${this.name}`)
}
} let firstOne = new Empoloy();
firstOne.sayHello(); //my name is Fred

TS学习之类的更多相关文章

  1. TS学习随笔(七)->声明文件

    now我们来看一看TS怎么声明文件, 在JS里面我们经常会使用各种第三方类库,引入方式也不太相同,常见的就是在HTML中通过script标签引入,然后就可以使用全局变量$或者jQuery了 我们通常这 ...

  2. TS学习随笔(三)->接口

    终于来到了比较重要的知识,接口,有多重要呢,反正是很重要好啵 在 TypeScript 中,我们使用接口(Interfaces)来定义对象的类型. 那什么是接口呢,在面向对象语言中,接口(Interf ...

  3. TS学习随笔(四)->数组的类型

    少侠们,今天我们继续来搞一搞TS 今天我们要来看一看TS中数组的定义是个什么鬼样子 数组的类型: 在 TypeScript 中,数组类型有多种定义方式,比较灵活.下面我们来看看有哪些定义方法 「类型 ...

  4. TS学习随笔(一)->安装和基本数据类型

    去年学过一段时间的TS,但由于在工作中不常用.就生疏了,最近项目要求用TS,那我就再回去搞搞TS,写一篇记录一下自己学习TS的进度以及TS知识点 首先,关于TS的定义我就不在这描述了,想看百度一下你就 ...

  5. TS学习

    随着vue3.0的即将到来,是时候学习一下TS了 简介:TypeScript是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类 ...

  6. TS学习随笔(六)->断言

    now,我们来看一看TS里面的断言,听起来很上档次啊,其实看完你就发出惊叹,这就是断言啊 类型断言 类型断言(Type Assertion)可以用来手动指定一个值的类型 语法 <类型>值 ...

  7. TS学习随笔(五)->函数

    这篇文章我们来看一下TS里面的函数 函数声明 在 JavaScript 中,有两种常见的定义函数的方式——函数声明(Function Declaration)和函数表达式(Function Expre ...

  8. TS学习随笔(二)->类型推论,联合类型

    这篇内容指南:        -----类型推论  -----联合类型 类型推论 第一篇中我们看了TS的基本使用和基本数据类型的使用,知道了变量在使用的时候都得加一个类型,那我们可不可以不加呢,这个嘛 ...

  9. TS学习之for..of

    for..of会遍历可迭代的对象,调用对象上的Symbol.iterator方法(可迭代对象,数组,字符串等) let arr = ["hello", "ts" ...

  10. TS学习之泛型

    可以使用泛型来创建可重用的组件,一个组件可以支持多种类型的数据 不适用泛型的函数 function myfn(args: number): number { return args; } functi ...

随机推荐

  1. linux c编程:进程环境

    一 进程终止: ⼀个进程可以登记若⼲个(具体⾃⼰验证⼀下)个函数,这些函数由exit⾃动调⽤,这些函数被称为终⽌处理函数, atexit函数可以登记这些函数. exit调⽤终⽌处理函数的顺序和atex ...

  2. python+NLTK 自然语言学习处理七:N-gram标注

    在上一章中介绍了用pos_tag进行词性标注.这一章将要介绍专门的标注器. 首先来看一元标注器,一元标注器利用一种简单的统计算法,对每个标识符分配最有可能的标记,建立一元标注器的技术称为训练. fro ...

  3. linux 中解压与压缩 常用操作详细讲解

    平时有时候 会在服务器进行一些文件的操作,比如安装一些服务与软件等等,都有解压操作,一般在 导出一些简单的服务器文件,也是先压缩后再导出,因此,在这里根据平时用到解压与压缩命令的频率来记录下: 1.最 ...

  4. Python基础(3)_可变对象与不可变对象、列表、元祖和字典

    可变对象与不可变对象 实例被创建后,身份和类型是不可变的, 如果值是不可以被修改的,则是不可变对象 如果值是可以被修改的,则是可变对象 #在id不动,type也不动,value被修改了,则称为可变 # ...

  5. Data Structure Array: Maximum sum such that no two elements are adjacent

    http://www.geeksforgeeks.org/maximum-sum-such-that-no-two-elements-are-adjacent/ #include <iostre ...

  6. RedisTemplate操作Redis

    RedisTemplate Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序 ...

  7. python内置方法补充all

    all(iterable) 版本:该函数在python2.5版本首次出现,适用于2.5以上版本,包括python3,兼容python3版本. 说明:如果iterable的所有元素不为0.''.Fals ...

  8. uboot 2013.01 代码简析(3)第二阶段初始化

    u-boot第二阶段初始化内容的入口函数是_main,_main位于arch/arm/lib/crt0.S文件中: _main函数中先为调用board_init_f准备初始化环境(设置栈指针sp和并给 ...

  9. 《python基础教程(第二版)》学习笔记 文件和素材(第11章)

    <python基础教程(第二版)>学习笔记 文件和素材(第11章) 打开文件:open(filename[,mode[,buffering]]) mode是读写文件的模式f=open(r' ...

  10. C程序员必须知道的内存知识【英】

    C程序员必须知道的内存知识[英] 时间 2015-03-08 14:16:11  极客头条原文  http://marek.vavrusa.com/c/memory/2015/02/20/memory ...