java 构造type_TypeScript中的构造函数重载

注意:这已经过简化并在4/13/2017更新以反映TypeScript 2.1,请参阅TypeScript 1.8答案的历史记录 .听起来您希望object参数是可选的,并且对象中的每个属性都是可选的 . 在示例中,如提供的那样,重载语法isn 't needed. I wanted to point out some bad practices in the some of the answer...
摘要由CSDN通过智能技术生成

注意:这已经过简化并在4/13/2017更新以反映TypeScript 2.1,请参阅TypeScript 1.8答案的历史记录 .

听起来您希望object参数是可选的,并且对象中的每个属性都是可选的 . 在示例中,如提供的那样,重载语法isn 't needed. I wanted to point out some bad practices in the some of the answers here. Granted, it'不是本质上写入 box = { x: 0, y: 87, width: 4, height: 0 } 的最小可能表达式,但是这提供了所有可能想要从类中描述的细节的代码 . 此示例允许您使用一个,一些,全部或没有参数调用函数,并仍然获取默认值 .

/** @class */

class Box {

public x?: number;

public y?: number;

public height?: number;

public width?: number;

// The class can work double-duty as the interface here since they are identical

// Alternately, reference your own interface, e.g.: `...BoxI = {} as BoxI`

constructor(obj: Box = {} as Box) {

// Define the properties of the incoming `obj` object here.

// Setting a default value with

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值