[ts]类
// 类 class Greeter { greeting: string; constructor(message: string) { this.greeting = message; } greet() { return `Hello, ${this.greeting}`; } } const greeter = new Greeter('world'); console.log(greeter.greet()); // 继承 class Animal { name: string; constructor(theName: string) { this.name = theName; } move(distance: number = 0) { console.log(`Animal moved ${distance}m.`); } } class Snake extends Animal { constructor(name: string) { super(name); }; move(distance: number = 5) { console.log('slithering'); super.move(distance); } } const sam = new Snake('sammy') sam.move(); // 公共,私有与受保护的修饰符 // 默认public // private 私有不能在外部访问 // protected 不能在类外访问,但是可以在子类内访问 class Person { protected name: string; protected constructor(name: string) { // 构造函数protected,类不能被实例化 this.name = name; } } class Employee extends Person { constructor(name: string) { super(name); } public getName(): string { return this.name; // 子类可以访问protected } } // readonly class Octopus { constructor(readonly name: string){} } const octopus = new Octopus('tom'); console.log(octopus.name); // 存取器 class Emp { constructor(private _fullname: string = '') {} get fullName(): string { return this._fullname; } set fullName(newName: string) { this._fullname = newName + 'aaa'; } } const emp = new Emp(); emp.fullName = 'jerry'; console.log(emp.fullName); // 静态属性 class Grid { static origin = { x: 0, y: 0 }; } console.log(Grid.origin); // 抽象类 abstract class Animal2 { abstract printMethod(): void; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY