Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cincom Visualworks Smalltalk-类方法初始化_Smalltalk - Fatal编程技术网

Cincom Visualworks Smalltalk-类方法初始化

Cincom Visualworks Smalltalk-类方法初始化,smalltalk,Smalltalk,对于这个新手问题,我深表歉意,但尽管我尝试了很长时间,还是没能找到答案 我使用Cincom Visualworks中的NewClass功能创建了一个矩阵类 Smalltalk.Core defineClass: #Matrix superclass: #{Core.Object} indexedType: #none private: false instanceVariableNames: 'rowCount columnCount cellValues '

对于这个新手问题,我深表歉意,但尽管我尝试了很长时间,还是没能找到答案

我使用Cincom Visualworks中的NewClass功能创建了一个矩阵类

Smalltalk.Core defineClass: #Matrix
    superclass: #{Core.Object}
    indexedType: #none
    private: false
    instanceVariableNames: 'rowCount columnCount cellValues '
    classInstanceVariableNames: ''
    imports: ''
    category: ''
添加了以下类方法:

withRowCount: rowCount withColumnCount: columnCount withCellValues: cellValues
    ^self new rowCount: rowCount columnCount: columnCount cellValues: cellValues.
添加了以下访问器方法:

cellValues
    ^cellValues
cellValues: anObject
    cellValues := anObject
columnCount
    ^columnCount
columnCount: anObject
    columnCount := anObject
rowCount
    ^rowCount
rowCount: anObject
    rowCount := anObject
我在工作区中有以下代码:

|myMatrix|
myMatrix := Matrix rowCount: 5 columnCount: 5 cellValues: 5.
Transcript show: (myMatrix rowCount).
但编译器说消息未定义。 我想我的课堂教学方法没有达到预期效果。
有人能指出我哪里出了问题吗?

首先:
Matrix
没有
rowCount:columnCount:cellValues:
方法。您可能是指
矩阵中的rowcount:5和columnCount:5以及cellValues:5

其次,我认为方法返回最后一个表达式的值。因此,链接方法并不是这样工作的。(即使是这样,这仍然像是一条信息。)

您的类方法应该如下所示

withRowCount: rowCount withColumnCount: columnCount withCellValues: cellValues
    | newMatrix |
    newMatrix := self new.
    newMatrix rowCount: rowCount;
              columnCount: columnCount;
              cellValues: cellValues.
    ^newMatrix
将消息分解,并告诉Smalltalk将这三条消息全部发送到
newMatrix

然后你可以像这样使用它

|myMatrix|
myMatrix := Matrix withRowCount: 5 withColumnCount: 5 withCellValues: 5.
Transcript show: (myMatrix rowCount).

这是我提出的一个可怕的愚蠢问题!我的代码都很好,只是在class方法中没有添加“;”在参数之间!英雄联盟在这上面浪费了6个小时。withRowCount:rowCount withColumnCount:columnCount withCellValues:cellValues^新建rowCount:rowCount;columnCount:columnCount;cellValues:cellValues。