定义一个类,怎么在新建这个类的时候,只赋值这个类中某几个属性

定义一个类,怎么在新建这个类的时候,只赋值这个类中某几个属性


HarmonyOS
2024-01-30 21:33:52
2385浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
icegoblin

可以在定义构造器时,将非必填属性设置为可选参数,在定义类型时冒号前加问号。

export default class ContactData { 
  public name: string; 
  public address?: string; 
  public telephony?: number; 
  public email?: string; 
  public remarks?: string; 
 
  constructor(name: string, address?: string, telephony?: number, email?: string, remarks?: string) { 
    this.name = name; 
    this.address = address; 
    this.telephony = telephony; 
    this.email = email; 
    this.remarks = remarks; 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
分享
微博
QQ
微信
回复
2024-02-01 10:11:51


相关问题
HarmonyOS如何获取一个名称
1988浏览 • 4回复 待解决
如何封装一个通用commonEvent工具
2663浏览 • 1回复 待解决
判断是否实现了接口
1085浏览 • 1回复 待解决
HarmonyOS 需要一个axios封装工具
844浏览 • 1回复 待解决
DevEco Studio新建为啥不带public?
6870浏览 • 1回复 已解决