简体   繁体   English

打字稿初始化二维数组

[英]Typescript initialize two dimensional array

I have the following class: 我有以下课程:

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

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

}

This class contains two custom classes. 此类包含两个自定义类。

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>;
}

Reservation class contains ReservationDetail class which's have array list type of ReservationPerson. Reservation类包含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;
    }
}

This is my ReservationPerson class with it's constuctor. 这是我的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;
    }
}

I have a initialisation promlem. 我有一个初始化问题。

when i try to create ReservationIfo class instance using the following code,reservationPerson property null. 当我尝试使用以下代码创建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')])
    ]));

When i try the following code second element reservationPerson is not null. 当我尝试以下代码时,第二个元素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')])
    ]));

I think i have 0 index element problem. 我认为我有0个索引元素问题。

You need to add parameters to your classes to initialize it the way you are initializing. 您需要在类中添加参数以初始化方式对其进行初始化。

To create a Person object using the syntax you are using new Person(null, null, '', '', '', '', '', new Date(), '') add the properties as parameter properties: 要使用语法创建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
    ) { }
}

See documentation on parameter properties. 请参阅有关参数属性的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM