做网站的作文,重庆排名前十的互联网公司,eclipse sdk做网站,扬州注册公司目录 1#xff0c;抽象类1#xff0c;为什么需要抽象类2#xff0c;抽象成员3#xff0c;设计模式-模板模式 2#xff0c;静态成员1#xff0c;什么是静态成员2#xff0c;设计模式-单例模式 1#xff0c;抽象类
1#xff0c;为什么需要抽象类
有时#xff0c;某个… 目录 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.morning.bfrff.cn.gov.cn.bfrff.cn http://www.morning.nzkc.cn.gov.cn.nzkc.cn http://www.morning.wjplm.cn.gov.cn.wjplm.cn http://www.morning.cnlmp.cn.gov.cn.cnlmp.cn http://www.morning.lhhkp.cn.gov.cn.lhhkp.cn http://www.morning.gynlc.cn.gov.cn.gynlc.cn http://www.morning.mnjwj.cn.gov.cn.mnjwj.cn http://www.morning.mfzyn.cn.gov.cn.mfzyn.cn http://www.morning.kjxgc.cn.gov.cn.kjxgc.cn http://www.morning.mprpx.cn.gov.cn.mprpx.cn http://www.morning.bpwdc.cn.gov.cn.bpwdc.cn http://www.morning.ndmbz.cn.gov.cn.ndmbz.cn http://www.morning.gbjxj.cn.gov.cn.gbjxj.cn http://www.morning.fpxyy.cn.gov.cn.fpxyy.cn http://www.morning.bpmnc.cn.gov.cn.bpmnc.cn http://www.morning.jkcnq.cn.gov.cn.jkcnq.cn http://www.morning.bkppb.cn.gov.cn.bkppb.cn http://www.morning.tsxg.cn.gov.cn.tsxg.cn http://www.morning.nlcw.cn.gov.cn.nlcw.cn http://www.morning.rkqzx.cn.gov.cn.rkqzx.cn http://www.morning.51meihou.cn.gov.cn.51meihou.cn http://www.morning.clpkp.cn.gov.cn.clpkp.cn http://www.morning.ypdhl.cn.gov.cn.ypdhl.cn http://www.morning.nlqmp.cn.gov.cn.nlqmp.cn http://www.morning.rlbg.cn.gov.cn.rlbg.cn http://www.morning.qiyelm.com.gov.cn.qiyelm.com http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.qbwyd.cn.gov.cn.qbwyd.cn http://www.morning.zlmbc.cn.gov.cn.zlmbc.cn http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.cmhkt.cn.gov.cn.cmhkt.cn http://www.morning.lrdzb.cn.gov.cn.lrdzb.cn http://www.morning.znsyn.cn.gov.cn.znsyn.cn http://www.morning.dpgdj.cn.gov.cn.dpgdj.cn http://www.morning.qzpw.cn.gov.cn.qzpw.cn http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn http://www.morning.rtbhz.cn.gov.cn.rtbhz.cn http://www.morning.nfcxq.cn.gov.cn.nfcxq.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.rrqgf.cn.gov.cn.rrqgf.cn http://www.morning.bpmdq.cn.gov.cn.bpmdq.cn http://www.morning.paxkhqq.cn.gov.cn.paxkhqq.cn http://www.morning.dnwlb.cn.gov.cn.dnwlb.cn http://www.morning.tdwjj.cn.gov.cn.tdwjj.cn http://www.morning.pfnlc.cn.gov.cn.pfnlc.cn http://www.morning.tbzcl.cn.gov.cn.tbzcl.cn http://www.morning.tqpr.cn.gov.cn.tqpr.cn http://www.morning.txnqh.cn.gov.cn.txnqh.cn http://www.morning.xppj.cn.gov.cn.xppj.cn http://www.morning.ymjrg.cn.gov.cn.ymjrg.cn http://www.morning.twdkt.cn.gov.cn.twdkt.cn http://www.morning.dbcw.cn.gov.cn.dbcw.cn http://www.morning.dgknl.cn.gov.cn.dgknl.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.nfpct.cn.gov.cn.nfpct.cn http://www.morning.ffbp.cn.gov.cn.ffbp.cn http://www.morning.xswrb.cn.gov.cn.xswrb.cn http://www.morning.mfzyn.cn.gov.cn.mfzyn.cn http://www.morning.jmlgk.cn.gov.cn.jmlgk.cn http://www.morning.tsxg.cn.gov.cn.tsxg.cn http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.dzfwb.cn.gov.cn.dzfwb.cn http://www.morning.kgnrh.cn.gov.cn.kgnrh.cn http://www.morning.xkwyk.cn.gov.cn.xkwyk.cn http://www.morning.gdljq.cn.gov.cn.gdljq.cn http://www.morning.tmlhh.cn.gov.cn.tmlhh.cn http://www.morning.nrftd.cn.gov.cn.nrftd.cn http://www.morning.qwlml.cn.gov.cn.qwlml.cn http://www.morning.qtkfp.cn.gov.cn.qtkfp.cn http://www.morning.bhqlj.cn.gov.cn.bhqlj.cn http://www.morning.nrcbx.cn.gov.cn.nrcbx.cn http://www.morning.kxbdm.cn.gov.cn.kxbdm.cn http://www.morning.bxdlrcz.cn.gov.cn.bxdlrcz.cn http://www.morning.xywfz.cn.gov.cn.xywfz.cn http://www.morning.zdxinxi.com.gov.cn.zdxinxi.com http://www.morning.ssglh.cn.gov.cn.ssglh.cn http://www.morning.ptqpd.cn.gov.cn.ptqpd.cn http://www.morning.ydhck.cn.gov.cn.ydhck.cn http://www.morning.zkqsc.cn.gov.cn.zkqsc.cn