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

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:只读属性,可以在构造函数中初始化,但在类的外部不能被修改。
  • 参数属性:在构造函数中直接声明的属性,可以指定修饰符。

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

http://www.xdnf.cn/news/4981.html

相关文章:

  • pytest自动化测试框架搭建,并生成allure测试报告
  • 【C语言干货】一维数组传参本质
  • 如何用LOTO示波器测量变压器带宽?
  • 一篇文章讲清楚mysql的聚簇索引、非聚簇索引、辅助索引
  • BGA底部填充胶固化异常延迟或不固化原因分析及解决方案
  • 垃圾回收的三色标记算法
  • <el-cascader中多选多层级点击节点也选中
  • Harmonyos-属性修改器和更新器
  • 低代码云MES、轻量级部署、让智造更简单
  • 探索大语言模型(LLM):词袋法(Bag of Words)原理与实现
  • 参考文献怎么对齐操作
  • Python 基础知识
  • 网络流量分析 | Snort
  • LeetCode 216.组合总和 III:回溯算法实现与剪枝优化
  • SpringBoot快速入门WebSocket(​​JSR-356附Demo源码)
  • 为何Google广告频繁拒登?常见原因与解决方法
  • 图表制作-折线图堆叠
  • 允许别的电脑连接我电脑wsl下5001、5002端口
  • 枚举 · 例13-【模板】双指针
  • 《Scala基础》
  • DeepSeek 赋能金融:从智能分析到高效服务的全链路革新
  • WHAT - react-query(TanStack Query) vs swr 请求
  • VUE——自定义指令
  • LabVIEW 2019 与 NI VISA 20.0 安装及报错处理
  • IEEE PRMVAI Workshop 17 | 智能医疗数据分析与应用
  • Baklib云中台赋能企业内容智管
  • Kubernetes外部访问服务全攻略:生产级方案详解
  • 12.hbase 源码构建
  • PFC(Power Factor Correction)功率因数校正电路
  • 金蝶api对接沙箱环境python代码调试