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

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


HarmonyOS
2024-01-30 21:33:52
浏览
收藏 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; 
  } 
}
分享
微博
QQ
微信
回复
2024-02-01 10:11:51
相关问题
HarmonyOS如何获取一个名称
281浏览 • 2回复 待解决
如何封装一个通用commonEvent工具
1705浏览 • 1回复 待解决
判断是否实现了接口
225浏览 • 1回复 待解决
DevEco Studio新建为啥不带public?
5633浏览 • 1回复 已解决
相机开发支持 Java 吗?
2558浏览 • 1回复 待解决
HarmonyOS 怎么属性赋默认值
359浏览 • 1回复 待解决
getContext(this)能否定义中使用
2080浏览 • 1回复 待解决
c++实例化自定义并调用方法
175浏览 • 1回复 待解决