当前位置: 首页 > news >正文

TS-抽象类和静态成员

目录

  • 1,抽象类
    • 1,为什么需要抽象类
    • 2,抽象成员
    • 3,设计模式-模板模式
  • 2,静态成员
    • 1,什么是静态成员
    • 2,设计模式-单例模式

1,抽象类

1,为什么需要抽象类

有时,某个类只表示一个抽象的概念,主要用于提取子类的公共成员,而不是直接创建它的实例对象。此时,这个类可以作为抽象类。

实现:在类名前加 abstract,抽象类不可以通过 new 来创建实例对象。

2,抽象成员

有时,一些公共成员是必须存在的,但还不确定成员的值或具体实现。因为,需要一种强约束,让继承的子类必须实现这些成员。

也就是说,抽象类中定义的抽象成员,在子类中必须实现。

如果子类也是抽象类,则可以不实现父类中的抽象成员或方法。

子类的3种实现方式:

abstract class Chess {abstract readonly name: string;
}// 第1种
class Horse extends Chess {readonly name: string = "马";
}// 第2种
class Pao extends Chess {readonly name: string;constructor() {super();this.name = "炮";}
}// 第3种
class Soldier extends Chess {get name() {return "兵";}
}

3,设计模式-模板模式

当某个公共方法中,一部分是公共逻辑,一部分又是私有逻辑(需要再子类中实现)。

此时,可以不将该方法定义为抽象方法,而是将那部分私有逻辑定义为抽象方法,并在当前方法中调用即可。

这样的强约束,可以只关注子类需要实现的东西,其他的父类已经完成。

abstract class Chess {abstract readonly name: string;move(targetX: number, targetY: number): boolean {// 第1和第2步属于公共逻辑// 1,边界条件判断// 2,目标位置是否有己方棋子// 第3步属于私有逻辑:不同兵种的移动规则(需要在子类中实现)if (this.rule(targetX, targetY)) {console.log("移动成功");return true;} else {return false;}}protected abstract rule(targetX: number, targetY: number): boolean;
}class Horse extends Chess {protected rule(targetX: number, targetY: number): boolean {return true; // 简单实现,做测试使用。}readonly name: string = "马";
}const horse = new Horse();
horse.move(2, 3);

2,静态成员

1,什么是静态成员

指附着在类上的成员(相当于构造函数的属性),通过static修饰。

每个实例成员的相同属性,理论上是不一样的,所以那些相同的属性应该作为静态成员,附着在类上。

构造函数或非静态方法中的this指向实例对象,如果要访问当前类,直接使用类名即可。

静态方法中的this指向当前类。

class User {static users: User[] = [];constructor(public id: string, public pwd: string, public name: string, public age: number) {User.users.push(this);}static login(loginId: string, loginPwd: string): User | undefined {// 静态方法中 this 指向当前类return this.users.find((user) => user.id === loginId && user.pwd === loginPwd);}sayHello() {console.log(`我是${this.name}`);}
}new User("u1", "pwd1", "用户1", 18);
new User("u2", "pwd2", "用户2", 188);console.log(User.users);const user = User.login("u1", "pwd1");
if (user) {user.sayHello();
}

2,设计模式-单例模式

某些类的实例对象,在系统中只允许存在一个,为了避免开发者随意创建实例对象而产生预期之外的错误,可以使用单例模式进行强约束。

构造函数私有化或抽象类,都不允许 new 实例对象。

class User {private constructor() {}private static _user?: User;static createUser() {if (!this._user) {this._user = new User();}return this._user;}
}const u1 = User.createUser();
const u2 = User.createUser();
console.log(u1 === u2);

下面的方式也可以创建单例,但有一些缺陷。

  • 实例对象一开始就创建了,不是在需要时才创建。
  • 做不到在创建单例对象时,提前执行一些代码。
class User {private constructor() {}static readonly singleUser = new User()
}const u1 = User.singleUser;
const u2 = User.singleUser;
console.log(u1 === u2);

以上。

http://www.lryc.cn/news/350087.html

相关文章:

  • SharePoint 使用renderListDataAsStream方法查询list超过5000时的数据
  • 2024042001-计算机网络 - 物理层
  • 通过java将数据导出为PDF,包扣合并单元格操作
  • Java内存模式以及volatile关键字的使用
  • 每日5题Day3 - LeetCode 11 - 15
  • 路由器、交换机和网卡
  • 腾讯开源混元DiT文生图模型,消费级单卡可推理
  • shell脚本基础(if/else结构)
  • 万字长文破解 AI 图片生成算法-Stable diffusion (第一篇)
  • Linux---编辑器vim的认识与简单配置
  • lucene中Collector类、CollectorManager类区分和用法
  • Android之给Button上添加按压效果
  • python EEL + vue3.js 项目中如何把组件中的函数提升为全局函数
  • sqli-labs靶场第十四关
  • 【C语言】6.C语言VS实用调试技巧(1)
  • AIGC行业现在适合进入吗
  • ubuntu CUDA 驱动更新,版本更新,多CUDA版本管理
  • effective python学习笔记_类与接口
  • 如何去除字符串两侧的空白字符?
  • Flutter 中的 PageStorage 小部件:全面指南
  • 头歌实践教学平台:CG1-v2.0-直线绘制
  • Nacos+GateWay 搭建微服务架构
  • 【2024华为HCIP831 | 高级网络工程师之路】刷题日记(18)
  • 在抖音做电商,没有货源,不懂直播怎么办?分享一种解决方案!
  • 基于单片机的智能安防系统设计(32+4G+WIFI版)-设计说明书
  • 云服务器配置mysql允许被远程连接从而使用图形化界面
  • 【软件测试】需求概念|软件的⽣命周期|开发模型|测试模型
  • SQL中的LAG函数与LEAD函数用法
  • 数据结构------二叉树经典习题1
  • 汇聚荣:拼多多长期没有流量如何提高?