0
我有以下類:打字稿初始化二維數組
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;
}
}
我有一個initialisation promlem。
當我嘗試使用以下代碼創建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不爲空。
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索引元素的問題。
ReservationDetail類已性質reservationPerson和reservationService,也有構造函數這就是有這樣的特性:
要使用你正在使用
new Person(null, null, '', '', '', '', '', new Date(), '')
語法添加屬性的參數屬性創建Person
對象。整個初始化是這個新的ReservationInfo(新的Person(..),新的Reservation(...,新的ReservationDetail(..,[new ReservationPerson('a','b','c')],[new ReservationServices (null,'a','b','v','dv')]), new ReservationDetail(...,[new ReservationPerson('a','b','c')],[new ReservationServices(null,'a','b','v','dv')]) ]));這裏首先ReservationDetail沒有ReservationPerson,但是當我打印時有秒。 –