【发布时间】:2020-12-17 09:44:44
【问题描述】:
我正在尝试制作一个程序,让您计算学生公寓中的一个人应该为给定的发票支付多少费用。
起初我创建了一个 Invoice 和一个 Roomie 类,一切都很好。 继续这一点,我已经实现了 Apartment 类,它有一些属性,但对于程序的逻辑来说非常重要,房间数量。
我想要发生的事情是,您可以创建一个 Apartment (myHouse) 的实例,并且每次添加 Invoice 时都会获取它的值。
class Apartment {
constructor(id, adress, numberOfRooms) {
this.id = id;
this.adress = adress;
this.numberOfRooms = numberOfRooms;
this.roomies = [];
this.invoices = [];
}
addRoomie(roomie) {
this.roomies.push(roomie);
}
addInvoice(invoice) {
this.invoices.push(invoice);
}
}
class Invoice {
constructor(total, type, begin, end) {
//Ask for this!
this.total = total;
this.type = type;
this.begin = begin;
this.end = end;
this.payed = false;
this.totalFractionated = this.totalPerPerson();
this.debtors = {};
}
totalPerPerson() {
const { begin, end, total, numberOfRooms } = this;
const difference = end.getTime() - begin.getTime();
const daysOfInterval = difference / (1000 * 60 * 60 * 24);
const totalPerDay = total / daysOfInterval;
return totalPerDay / 5; // This 5 I wanted to be numberOfRooms specify on the Aparment class
}
当然,我可以把super 方法和所有东西都放在一起,但是我必须每次这样指定发票的值(numberOfRooms)。
【问题讨论】:
-
也许我误解了你提到的问题:你不想每次都打电话给 numberOfRooms ......你是使用
class Invoice extends Apartment还是只使用super?
标签: javascript oop inheritance