TS学习之类
定义类
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的成员不能被外部访问;比较带有
private或protected成员的类型时,两个类型兼容的条件是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学习之类的更多相关文章
- TS学习随笔(七)->声明文件
now我们来看一看TS怎么声明文件, 在JS里面我们经常会使用各种第三方类库,引入方式也不太相同,常见的就是在HTML中通过script标签引入,然后就可以使用全局变量$或者jQuery了 我们通常这 ...
- TS学习随笔(三)->接口
终于来到了比较重要的知识,接口,有多重要呢,反正是很重要好啵 在 TypeScript 中,我们使用接口(Interfaces)来定义对象的类型. 那什么是接口呢,在面向对象语言中,接口(Interf ...
- TS学习随笔(四)->数组的类型
少侠们,今天我们继续来搞一搞TS 今天我们要来看一看TS中数组的定义是个什么鬼样子 数组的类型: 在 TypeScript 中,数组类型有多种定义方式,比较灵活.下面我们来看看有哪些定义方法 「类型 ...
- TS学习随笔(一)->安装和基本数据类型
去年学过一段时间的TS,但由于在工作中不常用.就生疏了,最近项目要求用TS,那我就再回去搞搞TS,写一篇记录一下自己学习TS的进度以及TS知识点 首先,关于TS的定义我就不在这描述了,想看百度一下你就 ...
- TS学习
随着vue3.0的即将到来,是时候学习一下TS了 简介:TypeScript是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类 ...
- TS学习随笔(六)->断言
now,我们来看一看TS里面的断言,听起来很上档次啊,其实看完你就发出惊叹,这就是断言啊 类型断言 类型断言(Type Assertion)可以用来手动指定一个值的类型 语法 <类型>值 ...
- TS学习随笔(五)->函数
这篇文章我们来看一下TS里面的函数 函数声明 在 JavaScript 中,有两种常见的定义函数的方式——函数声明(Function Declaration)和函数表达式(Function Expre ...
- TS学习随笔(二)->类型推论,联合类型
这篇内容指南: -----类型推论 -----联合类型 类型推论 第一篇中我们看了TS的基本使用和基本数据类型的使用,知道了变量在使用的时候都得加一个类型,那我们可不可以不加呢,这个嘛 ...
- TS学习之for..of
for..of会遍历可迭代的对象,调用对象上的Symbol.iterator方法(可迭代对象,数组,字符串等) let arr = ["hello", "ts" ...
- TS学习之泛型
可以使用泛型来创建可重用的组件,一个组件可以支持多种类型的数据 不适用泛型的函数 function myfn(args: number): number { return args; } functi ...
随机推荐
- JDBC详解1
JDBC详解1 JDBC整体思维导图 JDBC入门 导jar包:驱动! 加载驱动类:Class.forName("类名"); 给出url.username.password,其中u ...
- 教你管理SQL数据库系列(1-4)
原文 教你管理 SQL Server 数据库(1)数据库的结构 http://bbs.51cto.com/thread-1084951-1.html教你管理 SQL Server 数据库(2)系统数 ...
- Function- 几个转换函数
几个转换函数 SJIS_DBC_TO_SBC 全角转半角SJIS_SBC_TO_DBC 半角转全角 CLPB_IMPORT :从剪贴板导入internal tableCLPB_EXPORT : 从in ...
- 股票技术指标中的VOL,KDJ,MACD,OBV,VR,DMA分别代表什么意思?很关键,谢谢
http://zhidao.baidu.com/link?url=glKK7n0JUgqgrvfx2Gzd937-5zZg1bC615MwAp0P_mrYDytnMUpjoOQgYU871ny8St1 ...
- POJ - 3414 Pots 【BFS】
题目链接 http://poj.org/problem?id=3414 题意 给出两个杯子 容量分别为 A B 然后给出C 是目标容量 有三种操作 1 将一个杯子装满 2.将一个杯子全都倒掉 3.将一 ...
- PAT 天梯赛 L2-012. 关于堆的判断 【Heap】【STL】
题目链接 https://www.patest.cn/contests/gplt/L2-012 思路 使用 STL 里面有关 Heap 的函数 std::make_heap将[start, end)范 ...
- Oracle分页总汇
Oracle分页总汇 select * from (select a.*,rownum row_num from (select * from mytable t order by t.id desc ...
- runtime-分类为什么不生成setter和getter
前言 前几天有人问我一个问题:为什么分类不能自动创建get set方法.老实说,笔者从来没有去思考过这个问题.于是这次通过代码实践跟runtime源码来探究这个问题. 准备工作 为了能减少输出类数据的 ...
- 【leetcode刷题笔记】3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 【leetcode刷题笔记】Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...