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

TypeScript 中,属性修饰符

在 TypeScript 中,属性修饰符(Property Modifiers)是用于修饰类的属性或方法的关键字,它们可以改变属性或方法的行为和访问权限。TypeScript 提供了三种主要的属性修饰符:publicprivateprotected。此外,还有 readonly 修饰符用于定义只读属性。

1. public(公共属性)

  • 含义public 表示属性或方法是公开的,可以在类的内部和外部被访问。
  • 默认行为:如果不显式指定修饰符,类的属性和方法默认为 public
示例:
class Person {public name: string;public age: number;constructor(name: string, age: number) {this.name = name;this.age = age;}public greet() {console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);}
}const person = new Person("Alice", 25);
console.log(person.name); // 输出:Alice
console.log(person.age); // 输出:25
person.greet(); // 输出:Hello, my name is Alice and I am 25 years old.

2. private(私有属性)

  • 含义private 表示属性或方法是私有的,只能在类的内部被访问,不能在类的外部被访问。
  • 用途:用于封装类的内部实现细节,防止外部代码直接访问或修改。
示例:
class Person {private name: string;private age: number;constructor(name: string, age: number) {this.name = name;this.age = age;}public greet() {console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);}
}const person = new Person("Alice", 25);
// console.log(person.name); // Error: Property 'name' is private and only accessible within the class 'Person'.
// console.log(person.age); // Error: Property 'age' is private and only accessible within the class 'Person'.
person.greet(); // 输出:Hello, my name is Alice and I am 25 years old.

3. protected(受保护的属性)

  • 含义protected 表示属性或方法是受保护的,只能在类的内部以及其子类中被访问,不能在类的外部被访问。
  • 用途:用于实现类的继承,允许子类访问父类的某些属性或方法,但不允许外部代码直接访问。
示例:
class Person {protected name: string;protected age: number;constructor(name: string, age: number) {this.name = name;this.age = age;}public greet() {console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);}
}class Employee extends Person {constructor(name: string, age: number) {super(name, age);}public work() {console.log(`${this.name} is working.`);}
}const employee = new Employee("Bob", 30);
// console.log(employee.name); // Error: Property 'name' is protected and only accessible within the class 'Person' and its subclasses.
employee.greet(); // 输出:Hello, my name is Bob and I am 30 years old.
employee.work(); // 输出:Bob is working.

4. readonly(只读属性)

  • 含义readonly 表示属性是只读的,可以在类的构造函数中初始化,但在类的外部不能被修改。
  • 用途:用于定义那些不需要被外部代码修改的属性,确保数据的不可变性。
示例:
class Person {readonly name: string;public age: number;constructor(name: string, age: number) {this.name = name;this.age = age;}public greet() {console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);}
}const person = new Person("Alice", 25);
console.log(person.name); // 输出:Alice
// person.name = "Bob"; // Error: Cannot assign to 'name' because it is a read-only property.
person.age = 26; // 正常

5. 参数属性

TypeScript 还支持在构造函数中直接声明参数属性,这些参数属性会自动成为类的成员,并且可以指定修饰符(publicprivateprotectedreadonly)。

示例:
class Person {constructor(public name: string, private age: number) {}public greet() {console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);}
}const person = new Person("Alice", 25);
console.log(person.name); // 输出:Alice
// console.log(person.age); // Error: Property 'age' is private and only accessible within the class 'Person'.
person.greet(); // 输出:Hello, my name is Alice and I am 25 years old.

6. 总结

  • public:公开属性或方法,可以在类的内部和外部被访问。
  • private:私有属性或方法,只能在类的内部被访问。
  • protected:受保护的属性或方法,只能在类的内部及其子类中被访问。
  • readonly:只读属性,可以在构造函数中初始化,但在类的外部不能被修改。
  • 参数属性:在构造函数中直接声明的属性,可以指定修饰符。

合理使用这些属性修饰符可以帮助我们更好地封装类的内部实现,确保代码的安全性和可维护性。

相关文章:

  • 解锁跨平台开发的新时代——Compose Multiplatform
  • 针对共享内存和上述windows消息机制 在C++ 和qt之间的案例 进行详细举例说明
  • PyTorch 版本、torchvision 版本和 Python 版本的对应关系
  • 每日一笑话(三)
  • AI(学习笔记第二课) 使用langchain进行AI开发
  • 智能边缘计算系统:基于Python的创新应用
  • openwrt之UCI 增删改查(add/get/set /add_list...)
  • 防浪涌光电隔离型RS-485集线器可蓝牙通信
  • kafka records deletion policy
  • 赋能金融科技创新,Telerik打造高效、安全的金融应用解决方案!
  • Electron 打包与发布指南:让你的应用运行在 Windows、macOS、Linux
  • 【Java 专题补充】流程控制语句
  • 蓝桥杯第十六届c组c++题目及个人理解
  • 每周靶点分享:Angptl3、IgE、ADAM9及文献分享:抗体的多样性和特异性以及结构的新见解
  • 基于大型语言模型的高效时间序列预测模型选择
  • 【网工第6版】第7章 网络操作系统与应用服务器③
  • Arm核的Ubuntu系统上安装Wireshark
  • 英语六级---2024.12 卷二 仔细阅读2
  • 第5章 深度学习和卷积神经网络
  • 如何将 Build at、Hash 和 Time git 的 Tag 号等构建信息,自动写入一个 JSON 文件
  • wordpress 小工具参数/广州网站优化页面
  • 卖磁铁的网站怎么做/seo排名赚官网
  • 怎么能在网上卖货/seo专业培训班
  • 云南省疾控中心最新提示/青岛网络优化费用
  • 徐州 商城网站建设/b站推广链接
  • 高级营销型网站建设/在线一键生成网页