簡體   English   中英

測試一個類的實例

[英]testing for instance of a class

我有一個尖銳的問題,我是一個新手,所以我在詢問基本知識。 我創建了一個班

Object subclass: #Course
instanceVariableNames: 'id name day time isTwoHoursLong'
classVariableNames: ''
poolDictionaries: ''
category: 'Kernel-Objects'

實現以下方法:

isTwoHoursLong: aBoolean
(aBoolean isMemberOf: Boolean) 
    ifFalse: [self error: 'invalid input value']
    ifTrue: [isTwoHoursLong:=aBoolean.].

aBoolean必須為true或false(Boolean的一個實例)。 現在我嘗試使用該方法:

|c1|
c1:=Course new.
c1 isTwoHoursLong:true.

但是由於某種原因,我陷入了ifFalse選項中,該選項會發送錯誤。 有人可以幫忙清理一下嗎?

嘗試使用isKindOf:而不是isMemberOf:
TrueFalseBooleanisKindOf:子類isKindOf:測試參數是接收方的類還是超類。

但我可能會完全省略此手動類型檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM