IT序号网

class之测试一个类的实例

freeliver54 2025年02月15日 编程语言 41 0

我有一个问题,我对它完全陌生,所以我问的是基础知识。我创建了一个类

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:
TrueFalseBoolean 的子类,isKindOf: 测试参数是类还是父类(super class)接收者。

但我可能会完全忽略这种手动类型检查。


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!