[英]Methods in Pharo
I am still learning Pharo, but it is a bit confusing. 我还在学习Pharo,但这有点令人困惑。 There two classes,
CarRental
and Car
, and a Test
class, CarRentalTest
. 有两个类,
CarRental
和Car
,以及一个Test
类, CarRentalTest
。
There are fixed number of cars, the same car cannot be rented twice, I have the code, but there is a mistake. 有固定数量的车,同一辆车不能租两次,我有代码,但有一个错误。
| carRental redPanda yellowPanda blackTesla |
carRental := CarRental new.
redPanda := Car panda.
yellowPanda := Car panda.
blackTesla := Car tesla.
carRental
addCar: redPanda;
addCar: yellowPanda;
addCar: blackTesla.
self assert: carRental availableCars size equals: 3.
carRental rent: redPanda days: 5.
self assert: carRental availableCars size equals: 2.
self assert: carRental rentedCars size equals: 1
I tried to initialize the availableCars
and rentedCard
methods, but there is still an issue. 我尝试初始化
availableCars
和rentedCard
方法,但仍然存在问题。
You need to keep track of rented cars, right? 你需要跟踪租来的汽车,对吧? To do that add the ivar
rented
to the CarRental
class and initialize it to an empty collection: 为此,将
rented
的ivar添加到CarRental
类并将其初始化为空集合:
rented := OrderedCollection new.
(in other words, include the line above in the #initialize
method - instance side.) (换句话说,在
#initialize
方法中包含上面的行 - 实例端。)
Now, every time you rent a car add it to the rented
collection: 现在,每次租车时都将其添加到
rented
集合中:
rent: aCar
rented add: aCar
and when the car is returned 当汽车返回时
return: aCar
rented remove: aCar
Also you can add the getter method which was missing so far 您还可以添加到目前为止缺少的getter方法
rentedCars
^rented
and compute the cars available for rent as 并计算可供出租的汽车
availableCars
^cars copyWithoutAll: rented
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.