TypeScript 中,属性修饰符
在 TypeScript 中,属性修饰符(Property Modifiers)是用于修饰类的属性或方法的关键字,它们可以改变属性或方法的行为和访问权限。TypeScript 提供了三种主要的属性修饰符:public
、private
和 protected
。此外,还有 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 还支持在构造函数中直接声明参数属性,这些参数属性会自动成为类的成员,并且可以指定修饰符(public
、private
、protected
或 readonly
)。
示例:
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
:只读属性,可以在构造函数中初始化,但在类的外部不能被修改。- 参数属性:在构造函数中直接声明的属性,可以指定修饰符。
合理使用这些属性修饰符可以帮助我们更好地封装类的内部实现,确保代码的安全性和可维护性。