[英]Overriding < in Pharo
我試圖覆蓋pharo中的'<'運算符,因為我想要一個已實現的類(TimeCal)的SortedCollection。
TimeCal具有以下變量:年月日時分鍾。
我的想法是將所有變量轉換為分鍾,然后將它們與<運算符接收的比較進行比較。 但是,我確實收到了錯誤
“BlockClosure(Object)>> doesNotUnderstand:#>”
這是我的代碼:
< comparand
| thisInMins comparandInMins |
thisInMins := [(year * 525600) + (month * 43829) + (day * 1440) + (hour * 60) + minute].
comparandInMins := [(comparand year * 525600) + (comparand month * 43829) + (comparand day * 1440) + (comparand hour * 60) + comparand minute].
(thisInMins > comparandInMins)
ifTrue: [true] ifFalse: [false]
我用來測試它的代碼:
time1 := TimeCal new.
time1 hour: 12.
time1 day: 12.
time1 month: 11.
time2 := TimeCal new.
time2 hour: 12.
time2 day: 12.
time2 month: 8.
testing := time1 < time2.
我不確定我所做的是否正確。 我找不到任何關於如何做的正確指南。
那這個呢
< other
year = other year ifFalse: [^year < other year].
month = other month ifFalse: [^month < other month].
day = other day ifFalse: [^day < other day].
hour = other hour ifFalse: [^hour < other hour].
^minute < other minute
如果您已正確初始化變量,這應該有效:
< comparand
| thisInMins comparandInMins |
thisInMins := (year * 525600) + (month * 43829) + (day * 1440) + (hour * 60) + minute.
comparandInMins := (comparand year * 525600) + (comparand month * 43829) + (comparand day * 1440) + (comparand hour * 60) + comparand minute.
^ thisInMins < comparandInMins
為什么你在分鍾計算周圍加上方括號?
另外看看這個: ifTrue: [true] ifFalse: [false]
。 如果某事是真的則返回true而如果某些事情是假則返回false似乎是不必要的步驟。 您可以直接返回分鍾比較的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.