鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
你可以使用 readonly
关键字将属性设置为只读的。 只读属性必须在声明时或构造函数里被初始化
xxxxxxxxxx
class Person {
readonly name: string;
constructor (theName: string) {
this.name = theName;
}
}
let p = new Person("iwen");
p.name = "Man with the 3-piece suit"; // 错误! name 是只读的.
注意如果 readonly
和其他访问修饰符同时存在的话,需要写在其后面
xxxxxxxxxx
class Person {
public readonly name: string;
constructor (theName: string) {
this.name = theName;
}
}
let p = new Person("iwen");
p.name = "Man with the 3-piece suit"; // 错误! name 是只读的.
实时效果反馈
1. 在TypeScript中,类属性声明只读属性关键字正确的是:
A public
B private
C protected
D readonly
答案
1=>D