怎样将视频代码上传至网站,近期时政热点新闻20条,交换链接营销案例,网站设计的需求分析TypeScript 中的类#xff08;Class#xff09;详解
TypeScript 的类#xff08;class#xff09;基于 ES6 类语法#xff0c;并添加了强大的静态类型支持、访问修饰符、抽象类、装饰器等特性#xff0c;使其更接近传统面向对象语言#xff08;如 Java/C##xff09;Class详解TypeScript 的类class基于 ES6 类语法并添加了强大的静态类型支持、访问修饰符、抽象类、装饰器等特性使其更接近传统面向对象语言如 Java/C#同时完全兼容 JavaScript。1. 基本类声明与实例化classPerson{name:string;// 实例属性需手动声明age:number;constructor(name:string,age:number){this.namename;this.ageage;}greet():void{console.log(Hello, Im${this.name});}}letalicenewPerson(Alice,30);alice.greet();// Hello, Im Alice参数属性简写推荐减少冗余classPerson{constructor(publicname:string,// 自动声明 public name 并赋值privateage:number// 自动声明 private age 并赋值){}greet():void{console.log(Hello, Im${this.name});// console.log(this.age); // OK在类内部可访问}}letbobnewPerson(Bob,25);console.log(bob.name);// OK// console.log(bob.age); // 错误private2. 访问修饰符Access Modifiers修饰符作用范围示例public默认所有地方可访问public name: stringprivate仅类内部可访问private secret: stringprotected类内部及子类可访问protected familyName: stringreadonly只读不能重新赋值可结合以上readonly id: numberclassEmployee{constructor(publicname:string,privatesalary:number,protecteddepartment:string,readonlyid:number){}getInfo(){console.log(this.salary);// OKconsole.log(this.department);// OK}}classManagerextendsEmployee{manage(){// console.log(this.salary); // 错误privateconsole.log(this.department);// OKprotected}}3. 静态成员Static Members属于类本身而不是实例classMathUtils{staticPI:number3.14159;staticcircleArea(radius:number):number{returnthis.PI*radius**2;}}console.log(MathUtils.PI);// 3.14159console.log(MathUtils.circleArea(5));// 通过类名调用4. 类继承Extends与 superclassAnimal{constructor(publicname:string){}move(distance:number0){console.log(${this.name}moved${distance}m.);}}classDogextendsAnimal{constructor(name:string,publicbreed:string){super(name);// 必须调用 super()}bark(){console.log(Woof!);}move(distance:number5){super.move(distance);// 调用父类方法console.log(Dog is running!);}}letdognewDog(Buddy,Golden);dog.bark();dog.move();// 先调用 Animal.move再输出 Dog is running!5. 抽象类Abstract Class不能直接实例化用于定义子类必须实现的成员abstractclassShape{abstractgetArea():number;// 抽象方法必须在子类实现move(x:number,y:number){console.log(Moved to (${x},${y}));}}classCircleextendsShape{constructor(publicradius:number){super();}getArea():number{returnMath.PI*this.radius**2;}}letcirclenewCircle(10);console.log(circle.getArea());// let shape new Shape(); // 错误不能实例化抽象类6. 类实现接口Implements类可以实现一个或多个接口interfacePrintable{print():void;}interfaceLoggable{log(message:string):void;}classDocumentimplementsPrintable,Loggable{constructor(publictitle:string){}print(){console.log(Printing${this.title});}log(message:string){console.log([${this.title}]${message});}}7. getter / setterclassRectangle{constructor(private_width:number,private_height:number){}getarea():number{returnthis._width*this._height;}setwidth(value:number){if(value0)thrownewError(宽度必须正数);this._widthvalue;}}letrectnewRectangle(10,5);console.log(rect.area);// 50调用 getterrect.width20;// 调用 setter8. 类作为类型使用classCar{constructor(publicbrand:string){}}letmyCar:CarnewCar(Tesla);functiondrive(vehicle:Car){console.log(Driving${vehicle.brand});}9. 装饰器Decorators—— 实验性特性需启用experimentalDecorators常用于框架如 Angular、NestJSfunctionsealed(target:Function){Object.seal(target);Object.seal(target.prototype);}sealedclassGreeter{greeting:string;constructor(message:string){this.greetingmessage;}}10. 最佳实践建议建议说明使用参数属性简写减少 constructor 冗余优先使用private和readonly封装性更好抽象类用于定义通用行为强制子类实现关键方法接口 类组合使用接口定义合约类实现细节静态成员用于工具方法/常量如配置、工厂方法开启strictPropertyInitialization强制非 undefined 属性在 constructor 初始化小结类特性速查表特性语法示例基本类class Person { constructor(public name: string) {} }访问修饰符private age: number静态成员static count: number 0继承class Dog extends Animal {}抽象类/方法abstract class Shape { abstract draw(): void; }实现接口class User implements Printable {}getter/setterget fullName(): string { return ... }TypeScript 的类系统结合了现代 JS 的灵活性和强类型检查是构建大型、可维护应用的核心工具尤其在 Angular、NestJS 等框架中广泛使用。如果您想深入某个部分如泛型类、装饰器实战、类与模块的结合、或设计模式实现请告诉我