SORU
3 EKİM 2012, ÇARŞAMBA


TypeScript kurucu aşırı yükleme

Artık yapılacak yapıcı TypeScript aşırı yükleme vardır. Dil belirtimi sayfa 64 (v 0.8), açıklamaları yapıcı aşırı tarif var, ama herhangi bir örnek kod verilmedi.

Çok temel bir sınıf bildirimi şu an deniyorum; bu gibi görünüyor

interface IBox {    
    x : number;
    y : number;
    height : number;
    width : number;
}

class Box {
    public x: number;
    public y: number;
    public height: number;
    public width: number;

    constructor(obj: IBox) {    
        this.x = obj.x;
        this.y = obj.y;
        this.height = obj.height;
        this.width = obj.width;
    }   

    constructor() {
        this.x = 0;
        this.y = 0;
        this.width = 0;
        this.height = 0;
    }
}

Ne zaman TSK BoxSample ile koştu.ts, açık olan yapıcı bir yinelenen tanımı -- atar. Herhangi bir yardım için teşekkür ederiz. Şimdiden teşekkürler.

CEVAP
3 EKİM 2012, ÇARŞAMBA


TypeScript ama sadece bir uygulama var ve uygulama ile aşırı uyumlu bir imza olmalı aşırı bildirebilirsiniz. Sizin örnekte, bu kolay gibi isteğe bağlı bir parametre ile yapılabilir

interface IBox {    
    x : number;
    y : number;
    height : number;
    width : number;
}

class Box {
    public x: number;
    public y: number;
    public height: number;
    public width: number;

    constructor(obj?: IBox) {    
        this.x = obj && obj.x || 0
        this.y = obj && obj.y || 0
        this.height = obj && obj.height || 0
        this.width = obj && obj.width || 0;
    }   
}

ya da daha genel bir kurucu ile iki aşırı gibi

interface IBox {    
    x : number;
    y : number;
    height : number;
    width : number;
}

class Box {
    public x: number;
    public y: number;
    public height: number;
    public width: number;

    constructor();
    constructor(obj: IBox); 
    constructor(obj?: any) {    
        this.x = obj && obj.x || 0
        this.y = obj && obj.y || 0
        this.height = obj && obj.height || 0
        this.width = obj && obj.width || 0;
    }   
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Random Act of Kindness Video Contest

    Random Act o

    18 EYLÜL 2012
  • majesticdubstep

    majesticdubs

    3 Kasım 2011
  • The Pet Collective

    The Pet Coll

    5 Ocak 2012