Typescript初始化二维数组

时间:2017-09-11 06:59:27

标签: javascript node.js angular typescript

我有以下课程:

export class ReservationInfo {
    public person: Person;
    public reservation: Reservation;

    constructor(person: Person, reservation: Reservation) {
        this.person = person;
        this.reservation = reservation;
    }

}

此类包含两个自定义类。

export class Person {
    public id: number;
    public personal_no: number;
    public first_name: string;
    public last_name: string;
    public email: string;
    public gender: string;
    public address: string;
    public birthdate: Date;
    public phone: string;
}
export class Reservation {
    public id: number;
    public create_date: Date;
    public update_date: Date;
    public personal_no: number;
    public status_id: string;
    public payment_status_id: string;
    public payment_type: string;
    public comment: string;
    public res_type: string;
    public reservationDetail: Array<ReservationDetail>;
}

预订类包含ReservationDetail类,它具有ReservationPerson的数组列表类型。

export class ReservationDetail {
    public id: number;
    public reservation_id: number;
    public create_date: Date;
    public update_date: Date;
    public room_id: number;
    public status_id: string;
    public start_date: Date;
    public end_date: Date;
    public payment_type: string;
    public adult: string;
    public child: string;
    public additional_bed: string;
    public payment_status: string;
    public category_id: number;
    public category_name: string;
    public extra_person: string;
    public reservationPerson: Array<ReservationPerson>;
    public reservationService: Array<ReservationServices>;
   constructor(id: number, reservation_id: number, create_date: Date, update_date: Date, status_id: string, room_id: number, start_date: Date, end_date: Date, category_id: number,
        reservationPerson: Array<ReservationPerson>, reservationService: Array<ReservationServices>) {
        this.id = id;
        this.reservation_id = reservation_id;
        this.create_date = create_date;
        this.update_date = update_date;
        this.status_id = status_id;
        this.room_id = room_id;
        this.start_date = start_date;
        this.end_date = end_date;
        this.category_id = category_id;
        this.reservationPerson = reservationPerson;
        this.reservationService = reservationService;
    }
}

这是我的ReservationPerson类及其构造函数。

export class ReservationPerson {
    public reservation_id: number;
    public person_id: string;
    public first_name: string;
    public last_name: string;

    constructor(person_id: string, first_name: string, last_name: string) {
        this.person_id = person_id;
        this.first_name = first_name;
        this.last_name = last_name;
    }
}

我有一个初始化的承诺。

当我尝试使用以下代码创建ReservationIfo类实例时,reservationPerson属性为null。

new ReservationInfo(new Person(null, null, '', '', '', '', '', new Date(), ''), new Reservation(null, null, null, null, null, [
      new ReservationDetail(null, null, null, null, null, null, null, null, null, [new ReservationPerson('a', 'b', 'c')], [new ReservationServices(null, 'a', 'b', 'v', 'dv')])
    ]));

当我尝试以下代码时,第二个元素reservationPerson不为null。

new ReservationInfo(new Person(null, null, '', '', '', '', '', new Date(), ''), new Reservation(null, null, null, null, null, [
      new ReservationDetail(null, null, null, null, null, null, null, null, null, [new ReservationPerson('a', 'b', 'c')], [new ReservationServices(null, 'a', 'b', 'v', 'dv')]),
      new ReservationDetail(null, null, null, null, null, null, null, null, null, [new ReservationPerson('a', 'b', 'c')], [new ReservationServices(null, 'a', 'b', 'v', 'dv')])
    ]));

我认为我有0个索引元素问题。

1 个答案:

答案 0 :(得分:0)

您需要在类中添加参数,以便按初始化的方式对其进行初始化。

使用您正在使用的语法Person创建new Person(null, null, '', '', '', '', '', new Date(), '')对象,将属性添加为参数属性:

export class Person {
    constructor(
        public id: number,
        public personal_no: number,
        public first_name: string,
        public last_name: string,
        public email: string,
        public gender: string,
        public address: string,
        public birthdate: Date,
        public phone: string
    ) { }
}

请参阅documentation on parameter properties.