本文是基于b站视频BV14i4y1o7YF的学习笔记。
typescript学习记录
// 枚举
enum PlayerState {
idle,
run,
death,
attach,
}
// 抽象
abstract class Person {
name: string = "";
abstract say(text: string): void;
}
// 接口
interface IPerson {
run(): void;
}
// 类,继承和接口
class Player extends Person implements IPerson {
// 静态属性
static intro: string = "this is player";
private _hp: number = 100;
state: PlayerState = PlayerState.idle;
// 构造方法
constructor(name: string) {
super();
this.name = name;
}
// 继承方法
say(text: string) {
console.log(`${this.name} say ${text}`);
}
// 接口实现
run() {
console.log(this.name + " is running");
}
// 属性寄存器
get hp() {
return this._hp;
}
set hp(value) {
if (value < 0) this._hp = 0;
else this._hp = value;
}
// 静态方法
static demo() {
console.log("this is static function");
}
}
Player.demo();
let player1 = new Player("jack");
player1.say("hi");
player1.run();
player1.hp -= 200;
console.log(player1.hp);
// 访问修饰符
class BaseClass {
// 公开
public param1: string = "";
// 当前类可使用
private param2: string = "";
// 外部无法使用
protected func1() { }
}
// 命名空间
namespace abyssdwan {
export class Demo {
name: string = "";
}
export function test() {
console.log("hello");
}
}
let demo = new abyssdwan.Demo();
// 泛型
function add<T>(num: T): T {
return num;
}
console.log(add<number>(1));
// 元组
let hero: [string, number] = ["a", 1];
// 数组
let arr1: number[] = [1, 2, 3];
let arr2: Array<number> = new Array<number>();
// 后面追加
arr1.push(1);
// 前面追加
arr1.unshift(0);
// 后面移除
arr1.pop();
// 位置移除
arr1.slice(0, 1);
// 前面移除
arr1.shift();
// 合并数组
arr1 = arr1.concat(arr2);
// 查找位置
let ind = arr1.lastIndexOf(1);
// 排序
arr1.sort();
arr1.reverse();
// 字典
let dic: { [key: string]: number } = { a: 1 };
// 回调
function fun(value: Function) {
return value();
}
console.log(fun(() => 1 + 1));
// 正则表达式
let reg = /\d{4}-\d{7}/g;
let phone = "1234-1234567";
console.log(reg.exec(phone));
// 单例模式
class SoundManager {
private constructor() { }
// static Instance = new SoundManager();
private static instance: SoundManager;
static Instance() {
// 懒加载,省内存
if (!SoundManager.instance) {
SoundManager.instance = new SoundManager();
}
return SoundManager.instance;
}
}
// SoundManager.Instance;
SoundManager.Instance();
// 代理模式
interface ICalc {
calc(num1: number, num2: number): number;
}
class Calc1 implements ICalc {
calc(num1: number, num2: number): number {
return num1 + num2;
}
}
class Calc2 implements ICalc {
calc(num1: number, num2: number): number {
return num1 - num2;
}
}
class CalcDemo {
agent!: ICalc;
getNum(num1: number, num2: number) {
console.log(this.agent.calc(num1, num2));
}
}
let calcDemo = new CalcDemo();
calcDemo.agent = new Calc1();
calcDemo.getNum(1, 2);
// 观察者模式
interface IObserver {
nameChanged(newName: string): void;
}
class NpcObserver implements IObserver {
nameChanged(newName: any) {
console.log("nameChanged " + newName);
}
}
class NpcDemo {
private _name: string = "";
observers: Array<IObserver> = new Array<IObserver>();
get name() {
return this._name;
}
set name(val) {
this._name = val;
// 发送消息
for (let observer of this.observers) {
observer.nameChanged(this._name);
}
}
}
let npcDemo = new NpcDemo();
let npcObserver = new NpcObserver();
npcDemo.observers.push(npcObserver);
npcDemo.name = "test";
// 工厂模式
enum CarType { Bmw, Audi }
class Car {
name: string = "";
static Create(carType: CarType): Car {
let car!: Car;
switch (carType) {
case CarType.Bmw:
car = new Bmw();
break;
case CarType.Audi:
car = new Audi();
break;
}
return car;
}
}
class Bmw extends Car { }
class Audi extends Car { }
let bmw = Car.Create(CarType.Bmw);
// 链表
class Chain {
name!: string;
constructor(name: string) { this.name = name; }
next!: Chain;
}
let chain = new Chain('a');
chain.next = new Chain('b');
chain.next.next = new Chain('c');
// 删除链表
chain.next = chain.next.next;
// 添加链表
let tempChain = new Chain('d');
tempChain.next = chain.next;
chain.next = tempChain;
while (chain) {
console.log(chain.name);
chain = chain.next;
}
抱歉,学了一半发现unity更香。
未完待续