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 ...
随机推荐
- 13.Django模版
没什么好说的,看官方文档 https://docs.djangoproject.com/en/1.9/ref/templates/builtins/
- 500 Lines or Less: A Template Engine(模板引擎)
介绍: 多数项目都是包含很多的逻辑处理,只有少部分的文字文本处理.编程语言非常擅长这类项目.但是有一些项目只包含了少量的逻辑处理,大量的文本数据处理.对于这些任务,我们期望有一个工具能够很好的处理这些 ...
- SVG嵌入HTML
将SVG图像嵌入到HTML文件有多种方法: 使用<iframe>元素来嵌入SVG图像 使用<img>元素来嵌入SVG图像 将SVG图像作为背景图像嵌入 直接使用<svg& ...
- html post
post请求对应的html页面 页面效果 html代码 <html> <body> <form method="post" > First na ...
- Yii2 如何更好的在页面注入CSS
首先 先添加一个widgets,代码如下(提示:使用时注意修改命名空间) <?php /** * User: yiqing * Date: 2014/12/15 * Time: 0:21 */ ...
- iOS 8以后 定位手动授权问题
ios8以后 都是手动授权定位权限 不过不处理这块 在ios8以后的系统就会默认永不授权 即关闭了定位权限 处理办法如下 .导入框架头文件 #import <CoreLocation/CoreL ...
- 【leetcode刷题笔记】Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 一步一步带你分析 requirejs
详细源代码一共就2000多行,来看我这篇分析的同学应该都下载下来了,好了,话不多说,开始: 代码的开头就出现3个全局变量: requirejs, require, define var require ...
- 《机器学习实战》学习笔记第十二章 —— FP-growth算法
主要内容: 一. FP-growth算法简介 二.构建FP树 三.从一颗FP树中挖掘频繁项集 一. FP-growth算法简介 1.上次提到可以用Apriori算法来提取频繁项集,但是Aprior ...
- POJ 2831 Can We Build This One:次小生成树【N^2预处理】
题目链接:http://poj.org/problem?id=2831 题意: 给你一个图,每条边有边权. 然后有q组询问(i,x),问你如果将第i条边的边权改为x,这条边是否有可能在新的最小生成树中 ...